mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 15:50:01 +00:00
Remove extra synchronization in Promise.all
This commit is contained in:
parent
23a3b629d6
commit
3b30a3fa70
2 changed files with 32 additions and 9 deletions
10
lib/init.lua
10
lib/init.lua
|
@ -205,15 +205,9 @@ function Promise.all(promises)
|
|||
-- Keep a count of resolved promises because just checking the resolved
|
||||
-- values length wouldn't account for promises that resolve with nil.
|
||||
local resolvedCount = 0
|
||||
local rejected = false
|
||||
|
||||
-- Called when a single value is resolved and resolves if all are done.
|
||||
local function resolveOne(i, ...)
|
||||
if rejected then
|
||||
-- Bail out if this promise has already been rejected.
|
||||
return
|
||||
end
|
||||
|
||||
resolvedValues[i] = ...
|
||||
resolvedCount = resolvedCount + 1
|
||||
|
||||
|
@ -230,9 +224,7 @@ function Promise.all(promises)
|
|||
resolveOne(i, ...)
|
||||
end,
|
||||
function(...)
|
||||
if not rejected then
|
||||
reject(...)
|
||||
end
|
||||
reject(...)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
|
|
@ -388,5 +388,36 @@ return function()
|
|||
expect(first).to.equal("baz")
|
||||
expect(second).to.equal("qux")
|
||||
end)
|
||||
|
||||
it("should only reject once", function()
|
||||
local rejectA
|
||||
local rejectB
|
||||
|
||||
local a = Promise.new(function(_, reject)
|
||||
rejectA = reject
|
||||
end)
|
||||
|
||||
local b = Promise.new(function(_, reject)
|
||||
rejectB = reject
|
||||
end)
|
||||
|
||||
local combinedPromise = Promise.all({a, b})
|
||||
|
||||
expect(combinedPromise:getStatus()).to.equal(Promise.Status.Started)
|
||||
|
||||
rejectA("foo", "bar")
|
||||
|
||||
expect(combinedPromise:getStatus()).to.equal(Promise.Status.Rejected)
|
||||
|
||||
rejectB("baz", "qux")
|
||||
|
||||
local resultLength, result = pack(combinedPromise:_unwrap())
|
||||
local success, first, second = unpack(result, 1, resultLength)
|
||||
|
||||
expect(resultLength).to.equal(3)
|
||||
expect(success).to.equal(false)
|
||||
expect(first).to.equal("foo")
|
||||
expect(second).to.equal("bar")
|
||||
end)
|
||||
end)
|
||||
end
|
Loading…
Reference in a new issue