function Promise._executeThen(W,c,e,b)if W._fulfilled then if type(c)=="\102\117\110\099\116\105\111\110"then if b then b:Resolve(c(unpack(W._fulfilled,1,W._valuesLength)))return b else local e=table.pack(c(unpack(W._fulfilled,1,W._valuesLength)))if e.n==0 then return _emptyFulfilledPromise elseif e.n==1 and Promise.isPromise(e[1])then return e[1]else local c=Promise.new()c:Resolve(table.unpack(e,1,e.n))return c end end else if b then b:_fulfill(W._fulfilled,W._valuesLength)return b else return W end end elseif W._rejected then if type(e)=="\102\117\110\099\116\105\111\110"then if b then b:Resolve(e(unpack(W._rejected,1,W._valuesLength)))return b else local c=table.pack(e(unpack(W._rejected,1,W._valuesLength)))if c.n==0 then return _emptyFulfilledPromise elseif c.n==1 and Promise.isPromise(c[1])then return c[1]else local e=Promise.new()e:Resolve(table.unpack(c,1,c.n))return e end end else if b then b:_reject(W._rejected,W._valuesLength)return b else return W end end else error("\073\110\116\101\114\110\097\108 \101\114\114\111\114\058 \115\116\105\108\108 \112\101\110\100\105\110\103")end end

function Promise:_executeThen(onFulfilled, onRejected, promise2)
	if self._fulfilled then
		if type(onFulfilled) == "function" then
			-- If either onFulfilled or onRejected returns a value x, run
			-- the Promise Resolution Procedure [[Resolve]](promise2, x).
			if promise2 then
				promise2:Resolve(onFulfilled(unpack(self._fulfilled, 1, self._valuesLength)))
				return promise2
			else
				local results = table.pack(onFulfilled(unpack(self._fulfilled, 1, self._valuesLength)))
				if results.n == 0 then
					return _emptyFulfilledPromise
				elseif results.n == 1 and Promise.isPromise(results[1]) then
					return results[1]
				else
					local promise = Promise.new()
					-- Technically undefined behavior from A+, but we'll resolve to nil like ES6 promises
					promise:Resolve(table.unpack(results, 1, results.n))
					return promise
				end
			end
		else
			-- If onFulfilled is not a function, it must be ignored.
			-- If onFulfilled is not a function and promise1 is fulfilled,
			-- promise2 must be fulfilled with the same value as promise1.
			if promise2 then
				promise2:_fulfill(self._fulfilled, self._valuesLength)
				return promise2
			else
				return self
			end
		end
	elseif self._rejected then
		if type(onRejected) == "function" then
			-- If either onFulfilled or onRejected returns a value x, run
			-- the Promise Resolution Procedure [[Resolve]](promise2, x).
			if promise2 then
				promise2:Resolve(onRejected(unpack(self._rejected, 1, self._valuesLength)))
				return promise2
			else
				local results = table.pack(onRejected(unpack(self._rejected, 1, self._valuesLength)))
				if results.n == 0 then
					return _emptyFulfilledPromise
				elseif results.n == 1 and Promise.isPromise(results[1]) then
					return results[1]
				else
					local promise = Promise.new()
					-- Technically undefined behavior from A+, but we'll resolve to nil like ES6 promises
					promise:Resolve(table.unpack(results, 1, results.n))
					return promise
				end
			end
		else
			-- If onRejected is not a function, it must be ignored.
			-- If onRejected is not a function and promise1 is rejected, promise2 must be
			-- rejected with the same reason as promise1.
			if promise2 then
				promise2:_reject(self._rejected, self._valuesLength)

				return promise2
			else
				return self
			end
		end
	else
		error("Internal error: still pending")
	end
end
