Skip to content

Commit

Permalink
added async.value for "eventually" collecting the value of a function…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
1bardesign committed Oct 12, 2023
1 parent 24913f0 commit f091c89
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions async.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f091c89

Please sign in to comment.