quickjs-wrapper icon indicating copy to clipboard operation
quickjs-wrapper copied to clipboard

How to set or get Async functions

Open WavePlayz opened this issue 1 year ago • 9 comments

WavePlayz avatar Sep 29 '24 00:09 WavePlayz

Can you give an example?

HarlonWang avatar Sep 29 '24 03:09 HarlonWang

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?

WavePlayz avatar Oct 03 '24 19:10 WavePlayz

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

HarlonWang avatar Oct 15 '24 02:10 HarlonWang

is there any possibility of this getting

WavePlayz avatar Oct 19 '24 01:10 WavePlayz

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();
    }

HarlonWang avatar Oct 21 '24 02:10 HarlonWang

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

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

WavePlayz avatar Oct 22 '24 06:10 WavePlayz

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.

HarlonWang avatar Oct 22 '24 06:10 HarlonWang

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?

WavePlayz avatar Oct 24 '24 02:10 WavePlayz

It is arg[0], you can retrive it

HarlonWang avatar Oct 24 '24 03:10 HarlonWang