function Promise.Tap(g,O,D)local Y=g:Then(O,D)if Y==g then return Y end if Y._fulfilled then return g elseif Y._rejected then return g elseif Y._pendingExecuteList then local function O()return g end return Y:Then(O,O)else error("\066\097\100 \114\101\115\117\108\116 \115\116\097\116\101")end end

function Promise:Tap(onFulfilled, onRejected)
	-- Run immediately like then, but we return something safer!
	local result = self:Then(onFulfilled, onRejected)
	if result == self then
		return result
	end

	-- Most of the time we can just return the same
	-- promise. But sometimes we need to yield
	-- for the result to finish, and then resolve that result to a new result
	if result._fulfilled then
		return self
	elseif result._rejected then
		return self
	elseif result._pendingExecuteList then
		-- Definitely the most expensive case, might be able to make this better over time
		local function returnSelf()
			return self
		end

		return result:Then(returnSelf, returnSelf)
	else
		error("Bad result state")
	end
end
