Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set or get Async functions #76

Open
WavePlayz opened this issue Sep 29, 2024 · 9 comments
Open

How to set or get Async functions #76

WavePlayz opened this issue Sep 29, 2024 · 9 comments

Comments

@WavePlayz
Copy link

No description provided.

@HarlonWang
Copy link
Owner

Can you give an example?

@WavePlayz
Copy link
Author

Can you give an example?

for example on browser

function func (delay) {
    return new Promise( r => setTimeout(r, delay) )
}
async function test() {
    await func( 1000 )
    console.log( '1s passed' )
}

if i were to define async function (func) on java side, how could i?

@HarlonWang
Copy link
Owner

Are you trying to define an asynchronous function on the Java side? Currently not supported.

@WavePlayz
Copy link
Author

is there any possibility of this getting

@HarlonWang
Copy link
Owner

If you want to define an asynchronous function on the Java side for use in JS, it's not supported yet. However, if you want to access an asynchronous function defined in JS from Java, you can refer to the following sample test code:

    @Test
    public void testNativeCallWithAsyncFuncResult() {
        QuickJSContext context = createContext();
        context.evaluate("async function test() {return \"123\";}");
        JSFunction test = context.getGlobalObject().getJSFunction("test");
        JSObject promise = (JSObject) test.call();
        JSFunction then = promise.getJSFunction("then");
        JSObject ret = (JSObject) then.call((JSCallFunction) args -> {
            System.out.println(args[0]);
            return null;
        });

        ret.release();
        then.release();
        promise.release();
        test.release();

        context.destroy();
    }

@WavePlayz
Copy link
Author

why this ? the returned value is ret or args[0]

then.call((JSCallFunction) args -> {
            System.out.println(args[0]);
            return null;
        });

@HarlonWang
Copy link
Owner

why this ? the returned value is ret or args[0]

then.call((JSCallFunction) args -> {
            System.out.println(args[0]);
            return null;
        });

Sorry, I didn't understand what your question is.

@WavePlayz
Copy link
Author

if the test function returns something, how and where do i retrive it? is it the Object ret or arg[0] which holds the value?

@HarlonWang
Copy link
Owner

It is arg[0], you can retrive it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants