function Promise.prototype._finally(M,L,c,U)if not U then M._unhandledRejection=false end return Promise._new(L,function(r,W)local A=r if c then A=createAdvancer(L,c,r,W)end if U then local L=A A=function(...)if M._status==Promise.Status.Rejected then return r(M)end return L(...)end end if M._status==Promise.Status.Started then table.insert(M._queuedFinally,A)else A(M._status)end end,M)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
