function Promise.prototype._finally(l,S,e,z)if not z then l._unhandledRejection=false end return Promise._new(S,function(E,s)local B=E if e then B=createAdvancer(S,e,E,s)end if z then local S=B B=function(...)if l._status==Promise.Status.Rejected then return E(l)end return S(...)end end if l._status==Promise.Status.Started then table.insert(l._queuedFinally,B)else B(l._status)end end,l)end function Promise.prototype.finally(e,S)assert(S==nil or type(S)=="\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 e:_finally(debug.traceback(nil,2),S)end Promise.prototype.Finally=Promise.prototype.finally

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
