diff options
author | Kim Alvefur <zash@zash.se> | 2019-09-29 18:42:35 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2019-09-29 18:42:35 +0200 |
commit | 24b178651d59bb841090eb3d83f907172c98e0f4 (patch) | |
tree | 8c1ff08d9f161e2725fd440fdea161421da0bbb4 | |
parent | 1a0b7e772f647db324618682695957773f415759 (diff) | |
download | prosody-24b178651d59bb841090eb3d83f907172c98e0f4.tar.gz prosody-24b178651d59bb841090eb3d83f907172c98e0f4.zip |
util.async: Add function for waiting on promises and unpacking the results
-rw-r--r-- | util/async.lua | 16 |
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; }; |