seven332 / quickjs-android

QuickJS Android wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I use promises ?

pierreolivier opened this issue · comments

I congratulate you on the work achieved, the lib will be very very useful to me.
Here is my issue, i cannot make promises working.

QuickJS quickJS = new QuickJS.Builder().build();
JSRuntime runtime = quickJS.createJSRuntime();
JSContext context = runtime.createJSContext();

Method logMethod = Method.create(Void.class, MainActivity.class.getMethod("log", new Class<?>[]{ String.class }));
context.getGlobalObject().setProperty("log", context.createJSFunction(this, logMethod));

context.evaluate("log('before');" +
    "Promise.resolve()" +
    "  .then(function() { log('in promise 1'); })" +
    "  .then(function() { log('in promise 2'); });", "test.js");

I should see these 3 lines of log but I'm only seeing before. What should I add to make it working ?

It's not supported. We need something like void js_std_loop(JSContext *ctx) in quickjs-lib.c to wait for the promise.

What if the promise never be fulfilled or resolved? Maybe we should wait for promises with timeout.

Oh thanks ! It is working great :D