function Promise.prototype._andThen(s,H,e,d)s._unhandledRejection=false return Promise._new(H,function(m,c)local B=m if e then B=createAdvancer(H,e,m,c)end local o=c if d then o=createAdvancer(H,d,m,c)end if s._status==Promise.Status.Started then table.insert(s._queuedResolve,B)table.insert(s._queuedReject,o)elseif s._status==Promise.Status.Resolved then B(unpack(s._values,1,s._valuesLength))elseif s._status==Promise.Status.Rejected then o(unpack(s._values,1,s._valuesLength))elseif s._status==Promise.Status.Cancelled then c(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"..H}))end end,s)end function Promise.prototype.andThen(d,H,e)assert(H==nil or type(H)=="\102\117\110\099\116\105\111\110"or H.__call~=nil,string.format(ERROR_NON_FUNCTION,"\080\114\111\109\105\115\101\058\097\110\100\084\104\101\110"))assert(e==nil or type(e)=="\102\117\110\099\116\105\111\110"or e.__call~=nil,string.format(ERROR_NON_FUNCTION,"\080\114\111\109\105\115\101\058\097\110\100\084\104\101\110"))return d:_andThen(debug.traceback(nil,2),H,e)end
--[[
	Creates a new promise that receives the result of this promise.

	The given callbacks are invoked depending on that result.
]]
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" or successHandler.__call ~= nil,
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)
	assert(
		failureHandler == nil or type(failureHandler) == "function" or failureHandler.__call ~= nil,
		string.format(ERROR_NON_FUNCTION, "Promise:andThen")
	)

	return self:_andThen(debug.traceback(nil, 2), successHandler, failureHandler)
end
