aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2021-03-25 13:51:16 +0000
committerMatthew Wild <mwild1@gmail.com>2021-03-25 13:51:16 +0000
commit3952fae8ce728d56b9ef2044e490d99a6c12f444 (patch)
treefc6be322593893ecf6b362a5836002395414fad1
parent85d3eb829bb48512ab27a4c4971403e1f2284fb0 (diff)
downloadprosody-3952fae8ce728d56b9ef2044e490d99a6c12f444.tar.gz
prosody-3952fae8ce728d56b9ef2044e490d99a6c12f444.zip
util.promise: Add support for arbitrary keys in all()/all_settled()
-rw-r--r--spec/util_promise_spec.lua37
-rw-r--r--util/promise.lua42
2 files changed, 64 insertions, 15 deletions
diff --git a/spec/util_promise_spec.lua b/spec/util_promise_spec.lua
index 6fb5d6bd..4be9b5a0 100644
--- a/spec/util_promise_spec.lua
+++ b/spec/util_promise_spec.lua
@@ -352,6 +352,23 @@ describe("util.promise", function ()
assert.spy(cb_err).was_called(1);
assert.equal("fail", result);
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);
describe("all_settled()", function ()
it("works with fulfilled promises", function ()
@@ -406,6 +423,26 @@ describe("util.promise", function ()
{ status = "rejected", reason = "this fails" };
}, result);
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);
describe("catch()", function ()
it("works", function ()
diff --git a/util/promise.lua b/util/promise.lua
index bcb7beda..98d313ba 100644
--- a/util/promise.lua
+++ b/util/promise.lua
@@ -91,37 +91,49 @@ end
local function all(promises)
return new(function (resolve, reject)
- local count, total, results = 0, #promises, {};
- for i = 1, total do
- promises[i]:next(function (v)
- results[i] = v;
- count = count + 1;
- if count == total then
+ local settled, results, loop_finished = 0, {}, false;
+ local total = 0;
+ for k, v in pairs(promises) do
+ total = total + 1;
+ v:next(function (value)
+ results[k] = value;
+ settled = settled + 1;
+ if settled == total and loop_finished then
resolve(results);
end
end, reject);
end
+ loop_finished = true;
+ if settled == total then
+ resolve(results);
+ end
end);
end
local function all_settled(promises)
return new(function (resolve)
- local count, total, results = 0, #promises, {};
- for i = 1, total do
- promises[i]:next(function (v)
- results[i] = { status = "fulfilled", value = v };
- count = count + 1;
- if count == total then
+ local settled, results, loop_finished = 0, {}, false;
+ local total = 0;
+ for k, v in pairs(promises) do
+ total = total + 1;
+ v:next(function (value)
+ results[k] = { status = "fulfilled", value = value };
+ settled = settled + 1;
+ if settled == total and loop_finished then
resolve(results);
end
end, function (e)
- results[i] = { status = "rejected", reason = e };
- count = count + 1;
- if count == total then
+ results[k] = { status = "rejected", reason = e };
+ settled = settled + 1;
+ if settled == total and loop_finished then
resolve(results);
end
end);
end
+ loop_finished = true;
+ if settled == total then
+ resolve(results);
+ end
end);
end