Comparison

spec/util_promise_spec.lua @ 11484:a0120e935442

util.promise: Add join() convenience method Usage: promise.join(p1, p2, function (result1, result2) [...] end)
author Matthew Wild <mwild1@gmail.com>
date Thu, 25 Mar 2021 14:28:38 +0000
parent 11483:24ce9d380475
child 11485:7d42ed3a8a40
comparison
equal deleted inserted replaced
11483:24ce9d380475 11484:a0120e935442
455 end); 455 end);
456 assert.spy(cb1).was_called(0); 456 assert.spy(cb1).was_called(0);
457 p:catch(cb1); 457 p:catch(cb1);
458 assert.spy(cb1).was_called(1); 458 assert.spy(cb1).was_called(1);
459 assert.same({ foo = true }, result); 459 assert.same({ foo = true }, result);
460 end);
461 end);
462 describe("join()", function ()
463 it("works", function ()
464 local r1, r2;
465 local res1, res2;
466 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
467
468 local p = promise.join(p1, p2, function (_res1, _res2)
469 res1, res2 = _res1, _res2;
470 return promise.resolve("works");
471 end);
472
473 local result;
474 local cb = spy.new(function (v)
475 result = v;
476 end);
477 p:next(cb);
478 assert.spy(cb).was_called(0);
479 r2("yep");
480 assert.spy(cb).was_called(0);
481 r1("nope");
482 assert.spy(cb).was_called(1);
483 assert.same("works", result);
484 assert.equals("nope", res1);
485 assert.equals("yep", res2);
460 end); 486 end);
461 end); 487 end);
462 it("promises may be resolved by other promises", function () 488 it("promises may be resolved by other promises", function ()
463 local r1, r2; 489 local r1, r2;
464 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end); 490 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);