Comparison

spec/util_promise_spec.lua @ 10922:7d3dbb9eb3eb

util.promise: Add all_settled, which follows semantics of allSettled from ES2020
author Matthew Wild <mwild1@gmail.com>
date Mon, 08 Jun 2020 14:01:02 +0100
parent 10297:da9f21a70e52
child 11483:24ce9d380475
comparison
equal deleted inserted replaced
10921:6eb5d2bb11af 10922:7d3dbb9eb3eb
349 assert.spy(cb_err).was_called(1); 349 assert.spy(cb_err).was_called(1);
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);
355 end);
356 describe("all_settled()", function ()
357 it("works with fulfilled promises", function ()
358 local p1, p2 = promise.resolve("yep"), promise.resolve("nope");
359 local p = promise.all_settled({ p1, p2 });
360 local result;
361 p:next(function (v)
362 result = v;
363 end);
364 assert.same({
365 { status = "fulfilled", value = "yep" };
366 { status = "fulfilled", value = "nope" };
367 }, result);
368 end);
369 it("works with pending promises", function ()
370 local r1, r2;
371 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
372 local p = promise.all_settled({ p1, p2 });
373
374 local result;
375 local cb = spy.new(function (v)
376 result = v;
377 end);
378 p:next(cb);
379 assert.spy(cb).was_called(0);
380 r2("yep");
381 assert.spy(cb).was_called(0);
382 r1("nope");
383 assert.spy(cb).was_called(1);
384 assert.same({
385 { status = "fulfilled", value = "nope" };
386 { status = "fulfilled", value = "yep" };
387 }, result);
388 end);
389 it("works when some promises reject", function ()
390 local r1, r2;
391 local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (_, reject) r2 = reject end);
392 local p = promise.all_settled({ p1, p2 });
393
394 local result;
395 local cb = spy.new(function (v)
396 result = v;
397 end);
398 p:next(cb);
399 assert.spy(cb).was_called(0);
400 r2("this fails");
401 assert.spy(cb).was_called(0);
402 r1("this succeeds");
403 assert.spy(cb).was_called(1);
404 assert.same({
405 { status = "fulfilled", value = "this succeeds" };
406 { status = "rejected", reason = "this fails" };
407 }, result);
354 end); 408 end);
355 end); 409 end);
356 describe("catch()", function () 410 describe("catch()", function ()
357 it("works", function () 411 it("works", function ()
358 local result; 412 local result;