Changeset

11485:7d42ed3a8a40

util.promise: all()/all_settled() pass through non-promise values
author Matthew Wild <mwild1@gmail.com>
date Thu, 25 Mar 2021 14:32:32 +0000
parents 11484:a0120e935442
children 11486:78d843faaffc
files spec/util_promise_spec.lua util/promise.lua
diffstat 2 files changed, 63 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_promise_spec.lua	Thu Mar 25 14:28:38 2021 +0000
+++ b/spec/util_promise_spec.lua	Thu Mar 25 14:32:32 2021 +0000
@@ -369,6 +369,21 @@
 			assert.spy(cb).was_called(1);
 			assert.same({ [true] = "nope", [false] = "yep" }, result);
 		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);
 	describe("all_settled()", function ()
 		it("works with fulfilled promises", function ()
@@ -443,6 +458,24 @@
 				bar = { status = "fulfilled", value = "yep" };
 			}, result);
 		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);
 	describe("catch()", function ()
 		it("works", function ()
--- a/util/promise.lua	Thu Mar 25 14:28:38 2021 +0000
+++ b/util/promise.lua	Thu Mar 25 14:32:32 2021 +0000
@@ -95,14 +95,18 @@
 		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);
+			if is_promise(v) then
+				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);
+			else
+				results[k] = v;
+			end
 		end
 		loop_finished = true;
 		if settled == total then
@@ -116,20 +120,24 @@
 		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[k] = { status = "rejected", reason = e };
-				settled = settled + 1;
-				if settled == total and loop_finished then
-					resolve(results);
-				end
-			end);
+			if is_promise(v) then
+				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[k] = { status = "rejected", reason = e };
+					settled = settled + 1;
+					if settled == total and loop_finished then
+						resolve(results);
+					end
+				end);
+			else
+				results[k] = v;
+			end
 		end
 		loop_finished = true;
 		if settled == total then