function Promise.prototype._andThen(A,R,C,w)A._unhandledRejection=false if A._status==Promise.Status.Cancelled then local R=Promise.new(function() end)R:cancel()return R end return Promise._new(R,function(E,U,Z)local Q=E if C then Q=createAdvancer(R,C,E,U)end local L=U if w then L=createAdvancer(R,w,E,U)end if A._status==Promise.Status.Started then table.insert(A._queuedResolve,Q)table.insert(A._queuedReject,L)Z(function()if A._status==Promise.Status.Started then table.remove(A._queuedResolve,table.find(A._queuedResolve,Q))table.remove(A._queuedReject,table.find(A._queuedReject,L))end end)elseif A._status==Promise.Status.Resolved then Q(unpack(A._values,1,A._valuesLength))elseif A._status==Promise.Status.Rejected then L(unpack(A._values,1,A._valuesLength))end end,A)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

	-- If we are already cancelled, we return a cancelled Promise
	if self._status == Promise.Status.Cancelled then
		local promise = Promise.new(function() end)
		promise:cancel()

		return promise
	end

	-- Create a new promise to follow this part of the chain
	return Promise._new(traceback, function(resolve, reject, onCancel)
		-- 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)

			onCancel(function()
				-- These are guaranteed to exist because the cancellation handler is guaranteed to only
				-- be called at most once
				if self._status == Promise.Status.Started then
					table.remove(self._queuedResolve, table.find(self._queuedResolve, successCallback))
					table.remove(self._queuedReject, table.find(self._queuedReject, failureCallback))
				end
			end)
		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))
		end
	end, self)
end
