aboutsummaryrefslogtreecommitdiffstats
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
commit24b178651d59bb841090eb3d83f907172c98e0f4 (patch)
tree8c1ff08d9f161e2725fd440fdea161421da0bbb4
parent1a0b7e772f647db324618682695957773f415759 (diff)
downloadprosody-24b178651d59bb841090eb3d83f907172c98e0f4.tar.gz
prosody-24b178651d59bb841090eb3d83f907172c98e0f4.zip
util.async: Add function for waiting on promises and unpacking the results
-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;
};