mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 06:04:35 +00:00
added experimental async:await api
This commit is contained in:
parent
5641b393e4
commit
40dfb3cf9f
30
async.lua
30
async.lua
@ -171,6 +171,36 @@ function async:add_interval(f, delay)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--await the result of a function or set of functions
|
||||||
|
--return the results
|
||||||
|
function async:await(to_call, args)
|
||||||
|
local single_call = false
|
||||||
|
if type(to_call) == "function" then
|
||||||
|
to_call = {to_call}
|
||||||
|
single_call = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local awaiting = #to_call
|
||||||
|
local results = {}
|
||||||
|
for i, v in ipairs(to_call) do
|
||||||
|
self:call(function(...)
|
||||||
|
table.insert(results, {v(...)})
|
||||||
|
awaiting = awaiting - 1
|
||||||
|
end, args)
|
||||||
|
end
|
||||||
|
|
||||||
|
while awaiting > 0 do
|
||||||
|
async.stall()
|
||||||
|
end
|
||||||
|
|
||||||
|
--unwrap
|
||||||
|
if single_call then
|
||||||
|
results = results[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
--static async operation helpers
|
--static async operation helpers
|
||||||
-- these are not methods on the async object, but are
|
-- these are not methods on the async object, but are
|
||||||
-- intended to be called with dot syntax on the class itself
|
-- intended to be called with dot syntax on the class itself
|
||||||
|
Loading…
Reference in New Issue
Block a user