mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 15:50:01 +00:00
Cancel finally returned promise if outer cancelled
This commit is contained in:
parent
afc245c4f1
commit
893c43a90c
2 changed files with 20 additions and 0 deletions
|
@ -1436,11 +1436,17 @@ function Promise.prototype:_finally(traceback, finallyHandler)
|
||||||
self._unhandledRejection = false
|
self._unhandledRejection = false
|
||||||
|
|
||||||
local promise = Promise._new(traceback, function(resolve, reject, onCancel)
|
local promise = Promise._new(traceback, function(resolve, reject, onCancel)
|
||||||
|
local handlerPromise
|
||||||
|
|
||||||
onCancel(function()
|
onCancel(function()
|
||||||
-- The finally Promise is not a proper consumer of self. We don't care about the resolved value.
|
-- The finally Promise is not a proper consumer of self. We don't care about the resolved value.
|
||||||
-- All we care about is running at the end. Therefore, if self has no other consumers, it's safe to
|
-- All we care about is running at the end. Therefore, if self has no other consumers, it's safe to
|
||||||
-- cancel. We don't need to hold out cancelling just because there's a finally handler.
|
-- cancel. We don't need to hold out cancelling just because there's a finally handler.
|
||||||
self:_consumerCancelled(self)
|
self:_consumerCancelled(self)
|
||||||
|
|
||||||
|
if handlerPromise then
|
||||||
|
handlerPromise:cancel()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local finallyCallback = resolve
|
local finallyCallback = resolve
|
||||||
|
@ -1449,6 +1455,8 @@ function Promise.prototype:_finally(traceback, finallyHandler)
|
||||||
local callbackReturn = finallyHandler(...)
|
local callbackReturn = finallyHandler(...)
|
||||||
|
|
||||||
if Promise.is(callbackReturn) then
|
if Promise.is(callbackReturn) then
|
||||||
|
handlerPromise = callbackReturn
|
||||||
|
|
||||||
callbackReturn
|
callbackReturn
|
||||||
:finally(function(status)
|
:finally(function(status)
|
||||||
if status ~= Promise.Status.Rejected then
|
if status ~= Promise.Status.Rejected then
|
||||||
|
|
|
@ -835,6 +835,18 @@ return function()
|
||||||
expect(finallyRan).to.equal(true)
|
expect(finallyRan).to.equal(true)
|
||||||
expect(andThenRan).to.equal(false)
|
expect(andThenRan).to.equal(false)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("should cancel returned promise if cancelled", function()
|
||||||
|
local internal = Promise.new(function() end)
|
||||||
|
|
||||||
|
local promise = Promise.resolve():finally(function()
|
||||||
|
return internal
|
||||||
|
end)
|
||||||
|
|
||||||
|
promise:cancel()
|
||||||
|
|
||||||
|
expect(internal:getStatus()).to.equal(Promise.Status.Cancelled)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("Promise.all", function()
|
describe("Promise.all", function()
|
||||||
|
|
Loading…
Reference in a new issue