diff util/async.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parents 558f0555ba02
children 542a9a503073
line wrap: on
line diff
--- a/util/async.lua	Thu Nov 05 22:27:17 2020 +0100
+++ b/util/async.lua	Thu Nov 05 22:31:25 2020 +0100
@@ -53,7 +53,7 @@
 			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 @@
 
 	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 @@
 	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;
 };