Remove extra synchronization in Promise.all

This commit is contained in:
Lucien Greathouse 2018-09-14 18:47:57 -07:00
parent 23a3b629d6
commit 3b30a3fa70
2 changed files with 32 additions and 9 deletions

View file

@ -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

View file

@ -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