Comparison

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
comparison
equal deleted inserted replaced
11482:671f6b867e0d 11483:24ce9d380475
350 r1("nope"); 350 r1("nope");
351 assert.spy(cb).was_called(0); 351 assert.spy(cb).was_called(0);
352 assert.spy(cb_err).was_called(1); 352 assert.spy(cb_err).was_called(1);
353 assert.equal("fail", result); 353 assert.equal("fail", result);
354 end); 354 end);
355 it("works with non-numeric keys", function ()
356 local r1, r2;
357 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
358 local p = promise.all({ [true] = p1, [false] = p2 });
359
360 local result;
361 local cb = spy.new(function (v)
362 result = v;
363 end);
364 p:next(cb);
365 assert.spy(cb).was_called(0);
366 r2("yep");
367 assert.spy(cb).was_called(0);
368 r1("nope");
369 assert.spy(cb).was_called(1);
370 assert.same({ [true] = "nope", [false] = "yep" }, result);
371 end);
355 end); 372 end);
356 describe("all_settled()", function () 373 describe("all_settled()", function ()
357 it("works with fulfilled promises", function () 374 it("works with fulfilled promises", function ()
358 local p1, p2 = promise.resolve("yep"), promise.resolve("nope"); 375 local p1, p2 = promise.resolve("yep"), promise.resolve("nope");
359 local p = promise.all_settled({ p1, p2 }); 376 local p = promise.all_settled({ p1, p2 });
402 r1("this succeeds"); 419 r1("this succeeds");
403 assert.spy(cb).was_called(1); 420 assert.spy(cb).was_called(1);
404 assert.same({ 421 assert.same({
405 { status = "fulfilled", value = "this succeeds" }; 422 { status = "fulfilled", value = "this succeeds" };
406 { status = "rejected", reason = "this fails" }; 423 { status = "rejected", reason = "this fails" };
424 }, result);
425 end);
426 it("works with non-numeric keys", function ()
427 local r1, r2;
428 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
429 local p = promise.all_settled({ foo = p1, bar = p2 });
430
431 local result;
432 local cb = spy.new(function (v)
433 result = v;
434 end);
435 p:next(cb);
436 assert.spy(cb).was_called(0);
437 r2("yep");
438 assert.spy(cb).was_called(0);
439 r1("nope");
440 assert.spy(cb).was_called(1);
441 assert.same({
442 foo = { status = "fulfilled", value = "nope" };
443 bar = { status = "fulfilled", value = "yep" };
407 }, result); 444 }, result);
408 end); 445 end);
409 end); 446 end);
410 describe("catch()", function () 447 describe("catch()", function ()
411 it("works", function () 448 it("works", function ()