function Promise.prototype._finally(r,h,b,I)if not I then r._unhandledRejection=false end return Promise._new(h,function(Q,l)local j=Q if b then j=createAdvancer(h,b,Q,l)end if I then local h=j j=function(...)if r._status==Promise.Status.Rejected then return Q(r)end return h(...)end end if r._status==Promise.Status.Started then table.insert(r._queuedFinally,j)else j(r._status)end end,r)end
--[[
	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
