aboutsummaryrefslogtreecommitdiffstats
path: root/util/async.lua
diff options
context:
space:
mode:
Diffstat (limited to 'util/async.lua')
-rw-r--r--util/async.lua23
1 files changed, 22 insertions, 1 deletions
diff --git a/util/async.lua b/util/async.lua
index 20397785..341128d2 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -53,7 +53,7 @@ local function runner_continue(thread)
return false;
end
call_watcher(runner, "error", debug.traceback(thread, err));
- runner.state, runner.thread = "ready", nil;
+ runner.state = "ready";
return runner:run();
elseif state == "ready" then
-- If state is 'ready', it is our responsibility to update runner.state from 'waiting'.
@@ -159,6 +159,10 @@ function runner_mt:run(input)
local q, thread = self.queue, self.thread;
if not thread or coroutine.status(thread) == "dead" then
+ --luacheck: ignore 143/coroutine
+ if thread and coroutine.close then
+ coroutine.close(thread);
+ end
self:log("debug", "creating new coroutine");
-- Create a new coroutine for this runner
thread = runner_create_thread(self.func, self);
@@ -246,9 +250,26 @@ local function ready()
return pcall(checkthread);
end
+local function wait_for(promise)
+ local async_wait, async_done = waiter();
+ local ret, err = nil, nil;
+ promise:next(
+ function (r) ret = r; end,
+ function (e) err = e; end)
+ :finally(async_done);
+ async_wait();
+ if ret then
+ return ret;
+ else
+ return nil, err;
+ end
+end
+
return {
ready = ready;
waiter = waiter;
guarder = guarder;
runner = runner;
+ wait = wait_for; -- COMPAT w/trunk pre-0.12
+ wait_for = wait_for;
};