You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-59Lines changed: 1 addition & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,7 +190,7 @@ npm test # ensure everything it's working fine
190
190
191
191
### Promises
192
192
193
-
Promises can be await'd from Lua with some caveats detailed in the below section. To await a Promise call `:await()` on it which will yield the Lua execution until the promise completes.
193
+
Promises can be await'd from Lua. To await a Promise call `:await()` on it which will yield the Lua execution until the promise completes.
194
194
195
195
```js
196
196
const { LuaFactory } =require('wasmoon')
@@ -206,61 +206,3 @@ try {
206
206
lua.global.close()
207
207
}
208
208
```
209
-
210
-
### Async/Await
211
-
212
-
It's not possible to await in a callback from JS into Lua. This is a limitation of Lua but there are some workarounds. It can also be encountered when yielding at the top-level of a file. An example where you might encounter this is a snippet like this:
213
-
214
-
```js
215
-
local res =sleep(1):next(function ()
216
-
sleep(10):await()
217
-
return 15
218
-
end)
219
-
print("res", res:await())
220
-
```
221
-
222
-
Which will throw an error like this:
223
-
224
-
```
225
-
Error: Lua Error(ErrorRun/2): cannot resume dead coroutine
226
-
at Thread.assertOk (/home/tstableford/projects/wasmoon/dist/index.js:409:23)
227
-
at Thread.<anonymous> (/home/tstableford/projects/wasmoon/dist/index.js:142:22)
228
-
at Generator.throw (<anonymous>)
229
-
at rejected (/home/tstableford/projects/wasmoon/dist/index.js:26:69)
230
-
```
231
-
232
-
Or like this:
233
-
234
-
```
235
-
attempt to yield across a C-call boundary
236
-
```
237
-
238
-
You can workaround this by doing something like below:
239
-
240
-
```lua
241
-
function async(callback)
242
-
return function(...)
243
-
local co = coroutine.create(callback)
244
-
local safe, result = coroutine.resume(co, ...)
245
-
246
-
return Promise.create(function(resolve, reject)
247
-
local function step()
248
-
if coroutine.status(co) == "dead" then
249
-
local send = safe and resolve or reject
250
-
return send(result)
251
-
end
252
-
253
-
safe, result = coroutine.resume(co)
254
-
255
-
if safe and result == Promise.resolve(result) then
0 commit comments