aboutsummaryrefslogtreecommitdiffstats
path: root/util/async.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-09-29 18:42:35 +0200
committerKim Alvefur <zash@zash.se>2019-09-29 18:42:35 +0200
commit6b4ed1a30aa2a8b7e07ba7397564f41af53565ba (patch)
tree8c1ff08d9f161e2725fd440fdea161421da0bbb4 /util/async.lua
parentfc0e6e197d28d5a37b89a12bf9ae3119d0ae69d6 (diff)
downloadprosody-6b4ed1a30aa2a8b7e07ba7397564f41af53565ba.tar.gz
prosody-6b4ed1a30aa2a8b7e07ba7397564f41af53565ba.zip
util.async: Add function for waiting on promises and unpacking the results
Diffstat (limited to 'util/async.lua')
-rw-r--r--util/async.lua16
1 files changed, 16 insertions, 0 deletions
diff --git a/util/async.lua b/util/async.lua
index 20397785..d338071f 100644
--- a/util/async.lua
+++ b/util/async.lua
@@ -246,9 +246,25 @@ local function ready()
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;
};