diff options
author | Matthew Wild <mwild1@gmail.com> | 2020-06-08 14:01:02 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2020-06-08 14:01:02 +0100 |
commit | fdf2f0e9f4775809f72c6c283038f2e48a50e081 (patch) | |
tree | e791aba46ac6b94d6525ef383319a3335ab67d96 /util | |
parent | c7f339117d74a9a57268c44d0b20354e674df0c5 (diff) | |
download | prosody-fdf2f0e9f4775809f72c6c283038f2e48a50e081.tar.gz prosody-fdf2f0e9f4775809f72c6c283038f2e48a50e081.zip |
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
Diffstat (limited to 'util')
-rw-r--r-- | util/promise.lua | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/util/promise.lua b/util/promise.lua index 0b182b54..6ebb0dd6 100644 --- a/util/promise.lua +++ b/util/promise.lua @@ -104,6 +104,27 @@ local function all(promises) end); end +local function all_settled(promises) + return new(function (resolve) + local count, total, results = 0, #promises, {}; + for i = 1, total do + promises[i]:next(function (v) + results[i] = { status = "fulfilled", value = v }; + count = count + 1; + if count == total then + resolve(results); + end + end, function (e) + results[i] = { status = "rejected", reason = e }; + count = count + 1; + if count == total then + resolve(results); + end + end); + end + end); +end + local function race(promises) return new(function (resolve, reject) for i = 1, #promises do @@ -149,6 +170,7 @@ return { resolve = resolve; reject = reject; all = all; + all_settled = all_settled; race = race; try = try; is_promise = is_promise; |