Changeset

10291:7b48b620164c

util.async: Add function for waiting on promises and unpacking the results
author Kim Alvefur <zash@zash.se>
date Sun, 29 Sep 2019 18:42:35 +0200
parents 10290:b2ce3300f26a
children 10292:8fb546e40756
files util/async.lua
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/async.lua	Sun Sep 29 17:34:47 2019 +0200
+++ b/util/async.lua	Sun Sep 29 18:42:35 2019 +0200
@@ -246,9 +246,25 @@
 	return pcall(checkthread);
 end
 
+local function wait(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;
 };