function Promise.prototype.andThen(G,b,S)G._unhandledRejection=false return Promise.new(function(g,f)local r=g if b then r=createAdvancer(b,g,f)end local d=f if S then d=createAdvancer(S,g,f)end if G._status==Promise.Status.Started then table.insert(G._queuedResolve,r)table.insert(G._queuedReject,d)elseif G._status==Promise.Status.Resolved then r(unpack(G._values,1,G._valuesLength))elseif G._status==Promise.Status.Rejected then d(unpack(G._values,1,G._valuesLength))elseif G._status==Promise.Status.Cancelled then f("\080\114\111\109\105\115\101 \105\115 \099\097\110\099\101\108\108\101\100")end end,G)end

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

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

		local failureCallback = reject
		if failureHandler then
			failureCallback = createAdvancer(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("Promise is cancelled")
		end
	end, self)
end
