function Promise.prototype._andThen(M,j,H,A)M._unhandledRejection=false return Promise._new(j,function(l,L)local E=l if H then E=createAdvancer(j,H,l,L)end local P=L if A then P=createAdvancer(j,A,l,L)end if M._status==Promise.Status.Started then table.insert(M._queuedResolve,E)table.insert(M._queuedReject,P)elseif M._status==Promise.Status.Resolved then E(unpack(M._values,1,M._valuesLength))elseif M._status==Promise.Status.Rejected then P(unpack(M._values,1,M._valuesLength))elseif M._status==Promise.Status.Cancelled then L(Error.new({error="\080\114\111\109\105\115\101 \105\115 \099\097\110\099\101\108\108\101\100",kind=Error.Kind.AlreadyCancelled;context="\080\114\111\109\105\115\101 \099\114\101\097\116\101\100 \097\116\n\n"..j}))end end,M)end function Promise.prototype.andThen(A,j,H)assert(j==nil or type(j)=="\102\117\110\099\116\105\111\110",string.format(ERROR_NON_FUNCTION,"\080\114\111\109\105\115\101\058\097\110\100\084\104\101\110"))assert(H==nil or type(H)=="\102\117\110\099\116\105\111\110",string.format(ERROR_NON_FUNCTION,"\080\114\111\109\105\115\101\058\097\110\100\084\104\101\110"))return A:_andThen(debug.traceback(nil,2),j,H)end Promise.prototype.AndThen=Promise.prototype.andThen Promise.prototype.Then=Promise.prototype.andThen

function Promise.prototype:_andThen(traceback, successHandler, failureHandler)
	self._unhandledRejection = false

	-- Create a new promise to follow this part of the chain
	return Promise._new(traceback, function(resolve, reject)
		-- Our default callbacks just pass values onto the next promise.
		-- This lets success and failure cascade correctly!

		local successCallback = resolve
		if successHandler then
			successCallback = createAdvancer(
				traceback,
				successHandler,
				resolve,
				reject
			)
		end

		local failureCallback = reject
		if failureHandler then
			failureCallback = createAdvancer(
				traceback,
				failureHandler,
				resolve,
				reject
			)
		end

		if self._status == Promise.Status.Started then
			-- If we haven't resolved yet, put ourselves into the queue
			table.insert(self._queuedResolve, successCallback)
			table.insert(self._queuedReject, failureCallback)
		elseif self._status == Promise.Status.Resolved then
			-- This promise has already resolved! Trigger success immediately.
			successCallback(unpack(self._values, 1, self._valuesLength))
		elseif self._status == Promise.Status.Rejected then
			-- This promise died a terrible death! Trigger failure immediately.
			failureCallback(unpack(self._values, 1, self._valuesLength))
		elseif self._status == Promise.Status.Cancelled then
			-- We don't want to call the success handler or the failure handler,
			-- we just reject this promise outright.
			reject(Error.new({
				error = "Promise is cancelled",
				kind = Error.Kind.AlreadyCancelled,
				context = "Promise created at\n\n" .. traceback,
			}))
		end
	end, self)
end

function Promise.prototype:andThen(successHandler, failureHandler)
	assert(
		successHandler == nil or type(successHandler) == "function",
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)
	assert(
		failureHandler == nil or type(failureHandler) == "function",
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)

	return self:_andThen(debug.traceback(nil, 2), successHandler, failureHandler)
end
Promise.prototype.AndThen = Promise.prototype.andThen
Promise.prototype.Then = Promise.prototype.andThen
