PetterS / quickjs

Thin Python wrapper of https://bellard.org/quickjs/
https://github.com/bellard/QuickJS
MIT License
177 stars 19 forks source link

Asynchronous code #62

Closed qwenger closed 2 years ago

qwenger commented 2 years ago

I have trouble getting asynchronous code (async/await, Promise's) to work. I tried several approaches without much luck:

ctx = quickjs.Context()

ctx.add_callable("print", print)

ctx.eval("""
async function a() {
    print(1 + await 1);
}

a();
""")
ctx.eval("""
Promise.resolve(3).then(
    r => print(r)
);
""")
f = quickjs.Function("f", """
var b = {};

async function a() {
    b.x = 1 + await 1;
}

a()

function f() {
    while (!b.x) {}
    return b.x;
}
""")

f()

The two first codes execute without printing anything. The third code hangs due to non-terminating busy waiting.

Note: the codes work correctly in qjs (quickjs' interactive prompt), of course replacing print with console.log in the first two examples.

qwenger commented 2 years ago

Note 2:

var b = {};

async function a() {
    b.x = 1;
}

a();

while (!b.x) {} // does not hang