added async.value for "eventually" collecting the value of a function that can return nothing for a while (eg querying another thread or whatever). not particularly happy with the name, await is probably a loaded term though

This commit is contained in:
Max Cahill 2023-10-12 16:15:15 +11:00
parent 24913f05b8
commit f091c89893

View File

@ -191,6 +191,18 @@ function async.wait(time)
end
end
--eventually get a result, inline
-- repeatedly calls the provided function until it returns something,
-- stalling each time it doesn't, returning the result in the end
function async.value(f)
local r = f()
while not r do
async.stall()
r = f()
end
return r
end
--make an iterator or search function asynchronous, stalling every n (or 1) iterations
--can be useful with functional queries as well, if they are done in a coroutine.
function async.wrap_iterator(f, stall, n)