Closed timstableford closed 3 years ago
I think I didn't quite understand the problem you solved, I tried to handle promises inside lua and still don't get it, and things like pass a function back doesn't work anymore:
engine.global.set('asyncCallback', async (input) => {
return Promise.resolve(input * 2)
})
const [asyncCallback] = await engine.doString(`
return asyncCallback
`)
console.log(await asyncCallback(10)) // []
Yeah, that is definitely a bug. The purpose is that if you had something like:
engine.global.set('sleep', (input) => {
return new Promise(resolve => setTimeout(resolve, input))
})
const [asyncMultiplier] = await engine.doString(`
return function (val)
sleep(1)
return val * 2
end
`)
await asyncMultiplier(5)
So it will be possible to yield from within callbacks.
In attempting to integrate it with your then/catch/await syntax I've found there's a lot that will have to change from this PR. I'll keep going out of interest and see where this ends up but yours may well end up better
Closing in favour of https://github.com/ceifa/wasmoon/pull/15
This takes on board your feedback about doString and makes it so functions returned from Lua can call async functions and generally makes it possible for any function call to yield and return the results on the caller thread.