From 2141d9d94a7b0c855a99e4444b093f80ee316f75 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 25 Mar 2021 14:28:38 +0000 Subject: util.promise: Add join() convenience method Usage: promise.join(p1, p2, function (result1, result2) [...] end) --- spec/util_promise_spec.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'spec') diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua index 4be9b5a0..79eec5e2 100644 --- a/spec/util_promise_spec.lua +++ b/spec/util_promise_spec.lua @@ -459,6 +459,32 @@ describe("util.promise", function () assert.same({ foo = true }, result); end); end); + describe("join()", function () + it("works", function () + local r1, r2; + local res1, res2; + local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); + + local p = promise.join(p1, p2, function (_res1, _res2) + res1, res2 = _res1, _res2; + return promise.resolve("works"); + end); + + 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("works", result); + assert.equals("nope", res1); + assert.equals("yep", res2); + end); + end); it("promises may be resolved by other promises", function () local r1, r2; local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); -- cgit v1.2.3