function Promise.prototype._finally(Z,e,Q,L)if not L then Z._unhandledRejection=false end return Promise._new(e,function(C,d)local r=C if Q then r=createAdvancer(e,Q,C,d)end if L then local e=r r=function(...)if Z._status==Promise.Status.Rejected then return C(Z)end return e(...)end end if Z._status==Promise.Status.Started then table.insert(Z._queuedFinally,r)else r(Z._status)end end,Z)end function Promise.prototype.finally(Q,e)assert(e==nil or type(e)=="\102\117\110\099\116\105\111\110",string.format(ERROR_NON_FUNCTION,"\080\114\111\109\105\115\101\058\102\105\110\097\108\108\121"))return Q:_finally(debug.traceback(nil,2),e)end Promise.prototype.Finally=Promise.prototype.finally
--[[
	Used to set a handler for when the promise resolves, rejects, or is
	cancelled. Returns a new promise chained from this promise.
]]
function Promise.prototype:_finally(traceback, finallyHandler, onlyOk)
	if not onlyOk then
		self._unhandledRejection = false
	end

	-- Return a promise chained off of this promise
	return Promise._new(traceback, function(resolve, reject)
		local finallyCallback = resolve
		if finallyHandler then
			finallyCallback = createAdvancer(traceback, finallyHandler, resolve, reject)
		end

		if onlyOk then
			local callback = finallyCallback
			finallyCallback = function(...)
				if self._status == Promise.Status.Rejected then
					return resolve(self)
				end

				return callback(...)
			end
		end

		if self._status == Promise.Status.Started then
			-- The promise is not settled, so queue this.
			table.insert(self._queuedFinally, finallyCallback)
		else
			-- The promise already settled or was cancelled, run the callback now.
			finallyCallback(self._status)
		end
	end, self)
end

function Promise.prototype:finally(finallyHandler)
	assert(
		finallyHandler == nil or type(finallyHandler) == "function",
		string.format(ERROR_NON_FUNCTION, "Promise:finally")
	)
	return self:_finally(debug.traceback(nil, 2), finallyHandler)
end
Promise.prototype.Finally = Promise.prototype.finally
