function Promise.prototype.finally(F,U)F._unhandledRejection=false return Promise.new(function(i,A)local y=i if U then y=createAdvancer(U,i,A)end if F._status==Promise.Status.Started then table.insert(F._queuedFinally,y)else y(F._status)end end,F)end

function Promise.prototype:finally(finallyHandler)
	self._unhandledRejection = false

	-- Return a promise chained off of this promise
	return Promise.new(function(resolve, reject)
		local finallyCallback = resolve
		if finallyHandler then
			finallyCallback = createAdvancer(finallyHandler, resolve, reject)
		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
