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 | 6b4ed1a30aa2a8b7e07ba7397564f41af53565ba (patch) | |
tree | 8c1ff08d9f161e2725fd440fdea161421da0bbb4 /util/async.lua | |
parent | fc0e6e197d28d5a37b89a12bf9ae3119d0ae69d6 (diff) | |
download | prosody-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.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; }; |