function Promise.prototype._finally(U,Y,s,G)if not G then U._unhandledRejection=false end return Promise._new(Y,function(n,X)local k=n if s then k=createAdvancer(Y,s,n,X)end if G then local Y=k k=function(...)if U._status==Promise.Status.Rejected then return n(U)end return Y(...)end end if U._status==Promise.Status.Started then table.insert(U._queuedFinally,k)else k(U._status)end end,U)end

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
