-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathwait_all.lua
More file actions
35 lines (31 loc) · 716 Bytes
/
wait_all.lua
File metadata and controls
35 lines (31 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
local co = coroutine
local function __join(thunks)
local len = #thunks
local done = 0
local acc = {}
local thunk = function(step)
if len == 0 then
return step()
end
for i, tk in ipairs(thunks) do
assert(type(tk) == 'function', 'thunk must be function')
local callback = function(...)
acc[i] = { ... }
done = done + 1
if done == len then
step(unpack(acc))
end
end
tk(callback)
end
end
return thunk
end
---Waits for list of async calls to be completed
---@param defer fun(callback: fun(result: any))
---@return any[]
local function wait_all(defer)
assert(type(defer) == 'table', 'type error :: expected table')
return co.yield(__join(defer))
end
return wait_all