function Promise.prototype._andThen(M,U,N,u)M._unhandledRejection=false return Promise._new(U,function(G,t)local L=G if N then L=createAdvancer(U,N,G,t)end local z=t if u then z=createAdvancer(U,u,G,t)end if M._status==Promise.Status.Started then table.insert(M._queuedResolve,L)table.insert(M._queuedReject,z)elseif M._status==Promise.Status.Resolved then L(unpack(M._values,1,M._valuesLength))elseif M._status==Promise.Status.Rejected then z(unpack(M._values,1,M._valuesLength))elseif M._status==Promise.Status.Cancelled then t(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"..U}))end end,M)end

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
