How to set or get Async functions
Can you give an example?
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?
Are you trying to define an asynchronous function on the Java side? Currently not supported.
is there any possibility of this getting
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();
}
why this ? the returned value is ret or args[0]
then.call((JSCallFunction) args -> {
System.out.println(args[0]);
return null;
});
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.
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?
It is arg[0], you can retrive it