ceifa / wasmoon

A real lua 5.4 VM with JS bindings made with webassembly
MIT License
496 stars 32 forks source link

how to use await() in coroutine? #117

Closed jinguo-yin closed 1 week ago

jinguo-yin commented 5 months ago

I use coroutine.create(func) create one coroutine, using :await() in func, when JS promise is resolved,but in coroutine' await() not return;

my code is: function LedFlashFunc() local isOn = true while true do wait(1000)
print('LedFlashFunc') if isOn then local result = LedOn():await() else LedOff():await() end isOn = not isOn end end

local co = coroutine.create(func) local succeed, result = coroutine.resume(co) if not succeed then print('LUA Error: ' .. tostring(result)) end

jinguo-yin commented 5 months ago

When I use this code in main thread is OK!

jinguo-yin commented 5 months ago

i use async solve this program, code is: async(LedOn()) this function called yin coroutine will normal return when promise resolve ;

jinguo-yin commented 5 months ago

how to block execute in coroutine, :await() only valid in doFile lua code , when coroutine.create child, in child await can not resume.

lontrr commented 4 months ago

I fixed it by wrapping the function I was calling in lua with async, and adding injectObjects and openingStandard Lib to createEngine