ceifa / wasmoon

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

how to use await() in coroutine? #117

Open jinguo-yin opened 3 weeks ago

jinguo-yin commented 3 weeks 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 3 weeks ago

When I use this code in main thread is OK!

jinguo-yin commented 3 weeks 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 3 weeks ago

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