Software /
code /
prosody
Diff
spec/util_promise_spec.lua @ 11483:24ce9d380475
util.promise: Add support for arbitrary keys in all()/all_settled()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 25 Mar 2021 13:51:16 +0000 |
parent | 10922:7d3dbb9eb3eb |
child | 11484:a0120e935442 |
line wrap: on
line diff
--- a/spec/util_promise_spec.lua Wed Mar 24 16:36:00 2021 +0100 +++ b/spec/util_promise_spec.lua Thu Mar 25 13:51:16 2021 +0000 @@ -352,6 +352,23 @@ assert.spy(cb_err).was_called(1); assert.equal("fail", result); end); + it("works with non-numeric keys", 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({ [true] = p1, [false] = 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({ [true] = "nope", [false] = "yep" }, result); + end); end); describe("all_settled()", function () it("works with fulfilled promises", function () @@ -406,6 +423,26 @@ { status = "rejected", reason = "this fails" }; }, result); end); + it("works with non-numeric keys", 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({ foo = p1, bar = 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({ + foo = { status = "fulfilled", value = "nope" }; + bar = { status = "fulfilled", value = "yep" }; + }, result); + end); end); describe("catch()", function () it("works", function ()