# HG changeset patch
# User Matthew Wild <mwild1@gmail.com>
# Date 1539861110 -3600
# Node ID 439cf3bbe5f330fab60b8f330f9b688b24c4303d
# Parent  cb88d729e98ddbe1180ca0bf105ea7fcd14bb6b2
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.

diff -r cb88d729e98d -r 439cf3bbe5f3 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