util.promise: Also support automatic resolution by returning a promise from an on_reject handler
authorMatthew Wild <mwild1@gmail.com>
Thu, 18 Oct 2018 12:11:50 +0100
changeset 9515 439cf3bbe5f3
parent 9514 cb88d729e98d
child 9516 4f4f9823bd1d
util.promise: Also support automatic resolution by returning a promise from an on_reject handler Originally unimplemented because it wasn't clear to me what the correct behaviour was, but the A+ spec is clear that both onFulfilled and onRejected may return a promise.
util/promise.lua
--- a/util/promise.lua	Thu Oct 18 12:04:14 2018 +0100
+++ b/util/promise.lua	Thu Oct 18 12:11:50 2018 +0100
@@ -46,7 +46,9 @@
 	local function _reject(e)
 		if resolved then return; end
 		resolved = true;
-		if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
+		if is_promise(e) then
+			e:next(new_resolve_functions(p));
+		elseif promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
 			p.reason = e;
 		end
 	end