Skip to content

Commit

Permalink
feat: add await.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondill committed Jul 24, 2024
1 parent a0e0445 commit a981292
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions await.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---Use like javascript's new Promise constructor (await(function(resolve) resolve(1) end))
---@generic A
---@param f fun(resolve: fun(a: A, ...: any))
---@return A, any
local function await(f)
local co = coroutine.running()
local ret
f(function(...)
if coroutine.status(co) == "running" then
ret = ret or table.pack(...)
else
coroutine.resume(co, ...)
end
end)
if ret then return table.unpack(ret, 1, ret.n) end
return coroutine.yield()
end
return await

0 comments on commit a981292

Please sign in to comment.