util.promise: Add support for arbitrary keys in all()/all_settled()

This commit is contained in:
Matthew Wild 2021-03-25 13:51:16 +00:00
parent 85d3eb829b
commit 3952fae8ce
2 changed files with 64 additions and 15 deletions

View file

@ -352,6 +352,23 @@ describe("util.promise", function ()
assert.spy(cb_err).was_called(1); assert.spy(cb_err).was_called(1);
assert.equal("fail", result); assert.equal("fail", result);
end); end);
it("works with non-numeric keys", function ()
local r1, r2;
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
local p = promise.all({ [true] = p1, [false] = p2 });
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({ [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 ()
@ -406,6 +423,26 @@ describe("util.promise", function ()
{ status = "rejected", reason = "this fails" }; { status = "rejected", reason = "this fails" };
}, result); }, result);
end); end);
it("works with non-numeric keys", function ()
local r1, r2;
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
local p = promise.all_settled({ foo = p1, bar = p2 });
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({
foo = { status = "fulfilled", value = "nope" };
bar = { status = "fulfilled", value = "yep" };
}, result);
end);
end); end);
describe("catch()", function () describe("catch()", function ()
it("works", function () it("works", function ()

View file

@ -91,37 +91,49 @@ end
local function all(promises) local function all(promises)
return new(function (resolve, reject) return new(function (resolve, reject)
local count, total, results = 0, #promises, {}; local settled, results, loop_finished = 0, {}, false;
for i = 1, total do local total = 0;
promises[i]:next(function (v) for k, v in pairs(promises) do
results[i] = v; total = total + 1;
count = count + 1; v:next(function (value)
if count == total then results[k] = value;
settled = settled + 1;
if settled == total and loop_finished then
resolve(results); resolve(results);
end end
end, reject); end, reject);
end end
loop_finished = true;
if settled == total then
resolve(results);
end
end); end);
end end
local function all_settled(promises) local function all_settled(promises)
return new(function (resolve) return new(function (resolve)
local count, total, results = 0, #promises, {}; local settled, results, loop_finished = 0, {}, false;
for i = 1, total do local total = 0;
promises[i]:next(function (v) for k, v in pairs(promises) do
results[i] = { status = "fulfilled", value = v }; total = total + 1;
count = count + 1; v:next(function (value)
if count == total then results[k] = { status = "fulfilled", value = value };
settled = settled + 1;
if settled == total and loop_finished then
resolve(results); resolve(results);
end end
end, function (e) end, function (e)
results[i] = { status = "rejected", reason = e }; results[k] = { status = "rejected", reason = e };
count = count + 1; settled = settled + 1;
if count == total then if settled == total and loop_finished then
resolve(results); resolve(results);
end end
end); end);
end end
loop_finished = true;
if settled == total then
resolve(results);
end
end); end);
end end