mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 05:37:39 +03:00
util.promise: Add all_settled, which follows semantics of allSettled from ES2020
This commit is contained in:
parent
625ec0a93f
commit
9e0186b0ed
2 changed files with 76 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue