util.promise: all()/all_settled() pass through non-promise values

This commit is contained in:
Matthew Wild 2021-03-25 14:32:32 +00:00
parent 2141d9d94a
commit fec0d44e89
2 changed files with 63 additions and 22 deletions

View file

@ -369,6 +369,21 @@ describe("util.promise", function ()
assert.spy(cb).was_called(1); assert.spy(cb).was_called(1);
assert.same({ [true] = "nope", [false] = "yep" }, result); assert.same({ [true] = "nope", [false] = "yep" }, result);
end); end);
it("passes through non-promise values", function ()
local r1;
local p1 = promise.new(function (resolve) r1 = resolve end);
local p = promise.all({ [true] = p1, [false] = "yep" });
local result;
local cb = spy.new(function (v)
result = v;
end);
p:next(cb);
assert.spy(cb).was_called(0);
r1("nope");
assert.spy(cb).was_called(1);
assert.same({ [true] = "nope", [false] = "yep" }, result);
end);
end); end);
describe("all_settled()", function () describe("all_settled()", function ()
it("works with fulfilled promises", function () it("works with fulfilled promises", function ()
@ -443,6 +458,24 @@ describe("util.promise", function ()
bar = { status = "fulfilled", value = "yep" }; bar = { status = "fulfilled", value = "yep" };
}, result); }, result);
end); end);
it("passes through non-promise values", function ()
local r1;
local p1 = promise.new(function (resolve) r1 = resolve end);
local p = promise.all_settled({ foo = p1, bar = "yep" });
local result;
local cb = spy.new(function (v)
result = v;
end);
p:next(cb);
assert.spy(cb).was_called(0);
r1("nope");
assert.spy(cb).was_called(1);
assert.same({
foo = { status = "fulfilled", value = "nope" };
bar = "yep";
}, result);
end);
end); end);
describe("catch()", function () describe("catch()", function ()
it("works", function () it("works", function ()

View file

@ -95,14 +95,18 @@ local function all(promises)
local settled, results, loop_finished = 0, {}, false; local settled, results, loop_finished = 0, {}, false;
local total = 0; local total = 0;
for k, v in pairs(promises) do for k, v in pairs(promises) do
total = total + 1; if is_promise(v) then
v:next(function (value) total = total + 1;
results[k] = value; v:next(function (value)
settled = settled + 1; results[k] = value;
if settled == total and loop_finished then settled = settled + 1;
resolve(results); if settled == total and loop_finished then
end resolve(results);
end, reject); end
end, reject);
else
results[k] = v;
end
end end
loop_finished = true; loop_finished = true;
if settled == total then if settled == total then
@ -116,20 +120,24 @@ local function all_settled(promises)
local settled, results, loop_finished = 0, {}, false; local settled, results, loop_finished = 0, {}, false;
local total = 0; local total = 0;
for k, v in pairs(promises) do for k, v in pairs(promises) do
total = total + 1; if is_promise(v) then
v:next(function (value) total = total + 1;
results[k] = { status = "fulfilled", value = value }; v:next(function (value)
settled = settled + 1; results[k] = { status = "fulfilled", value = value };
if settled == total and loop_finished then settled = settled + 1;
resolve(results); if settled == total and loop_finished then
end resolve(results);
end, function (e) end
results[k] = { status = "rejected", reason = e }; end, function (e)
settled = settled + 1; results[k] = { status = "rejected", reason = e };
if settled == total and loop_finished then settled = settled + 1;
resolve(results); if settled == total and loop_finished then
end resolve(results);
end); end
end);
else
results[k] = v;
end
end end
loop_finished = true; loop_finished = true;
if settled == total then if settled == total then