mirror of
https://github.com/bjc/prosody.git
synced 2025-04-03 21:27:38 +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
|
@ -353,6 +353,60 @@ describe("util.promise", function ()
|
|||
assert.equal("fail", result);
|
||||
end);
|
||||
end);
|
||||
describe("all_settled()", function ()
|
||||
it("works with fulfilled promises", function ()
|
||||
local p1, p2 = promise.resolve("yep"), promise.resolve("nope");
|
||||
local p = promise.all_settled({ p1, p2 });
|
||||
local result;
|
||||
p:next(function (v)
|
||||
result = v;
|
||||
end);
|
||||
assert.same({
|
||||
{ status = "fulfilled", value = "yep" };
|
||||
{ status = "fulfilled", value = "nope" };
|
||||
}, result);
|
||||
end);
|
||||
it("works with pending promises", function ()
|
||||
local r1, r2;
|
||||
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
|
||||
local p = promise.all_settled({ p1, p2 });
|
||||
|
||||
local result;
|
||||
local cb = spy.new(function (v)
|
||||
result = v;
|
||||
end);
|
||||
p:next(cb);
|
||||
assert.spy(cb).was_called(0);
|
||||
r2("yep");
|
||||
assert.spy(cb).was_called(0);
|
||||
r1("nope");
|
||||
assert.spy(cb).was_called(1);
|
||||
assert.same({
|
||||
{ status = "fulfilled", value = "nope" };
|
||||
{ status = "fulfilled", value = "yep" };
|
||||
}, result);
|
||||
end);
|
||||
it("works when some promises reject", function ()
|
||||
local r1, r2;
|
||||
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (_, reject) r2 = reject end);
|
||||
local p = promise.all_settled({ p1, p2 });
|
||||
|
||||
local result;
|
||||
local cb = spy.new(function (v)
|
||||
result = v;
|
||||
end);
|
||||
p:next(cb);
|
||||
assert.spy(cb).was_called(0);
|
||||
r2("this fails");
|
||||
assert.spy(cb).was_called(0);
|
||||
r1("this succeeds");
|
||||
assert.spy(cb).was_called(1);
|
||||
assert.same({
|
||||
{ status = "fulfilled", value = "this succeeds" };
|
||||
{ status = "rejected", reason = "this fails" };
|
||||
}, result);
|
||||
end);
|
||||
end);
|
||||
describe("catch()", function ()
|
||||
it("works", function ()
|
||||
local result;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue