mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 15:50:01 +00:00
Make error more robust when misusing all/race
This commit is contained in:
parent
213f7e3e64
commit
3dbb121906
2 changed files with 28 additions and 10 deletions
20
lib/init.lua
20
lib/init.lua
|
@ -211,19 +211,19 @@ function Promise.all(promises)
|
|||
error("Please pass a list of promises to Promise.all", 2)
|
||||
end
|
||||
|
||||
-- We need to check that each value is a promise here so that we can produce
|
||||
-- a proper error rather than a rejected promise with our error.
|
||||
for i, promise in pairs(promises) do
|
||||
if not Promise.is(promise) then
|
||||
error(("Non-promise value passed into Promise.all at index %s"):format(tostring(i)), 2)
|
||||
end
|
||||
end
|
||||
|
||||
-- If there are no values then return an already resolved promise.
|
||||
if #promises == 0 then
|
||||
return Promise.resolve({})
|
||||
end
|
||||
|
||||
-- We need to check that each value is a promise here so that we can produce
|
||||
-- a proper error rather than a rejected promise with our error.
|
||||
for i = 1, #promises do
|
||||
if not Promise.is(promises[i]) then
|
||||
error(("Non-promise value passed into Promise.all at index #%d"):format(i), 2)
|
||||
end
|
||||
end
|
||||
|
||||
return Promise.new(function(resolve, reject)
|
||||
-- An array to contain our resolved values from the given promises.
|
||||
local resolvedValues = {}
|
||||
|
@ -264,8 +264,8 @@ end
|
|||
function Promise.race(promises)
|
||||
assert(type(promises) == "table", "Please pass a list of promises to Promise.race")
|
||||
|
||||
for i, promise in ipairs(promises) do
|
||||
assert(Promise.is(promise), ("Non-promise value passed into Promise.race at index #%d"):format(i))
|
||||
for i, promise in pairs(promises) do
|
||||
assert(Promise.is(promise), ("Non-promise value passed into Promise.race at index %s"):format(tostring(i)))
|
||||
end
|
||||
|
||||
return Promise.new(function(resolve, reject, onCancel)
|
||||
|
|
|
@ -550,6 +550,15 @@ return function()
|
|||
expect(first).to.equal("foo")
|
||||
expect(second).to.equal("bar")
|
||||
end)
|
||||
|
||||
it("should error if a non-array table is passed in", function()
|
||||
local ok, err = pcall(function()
|
||||
Promise.all(Promise.new(function() end))
|
||||
end)
|
||||
|
||||
expect(ok).to.be.ok()
|
||||
expect(err:find("Non%-promise")).to.be.ok()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Promise.race", function()
|
||||
|
@ -581,6 +590,15 @@ return function()
|
|||
expect(promises[1]:getStatus()).to.equal(Promise.Status.Cancelled)
|
||||
expect(promises[2]:getStatus()).to.equal(Promise.Status.Resolved)
|
||||
end)
|
||||
|
||||
it("should error if a non-array table is passed in", function()
|
||||
local ok, err = pcall(function()
|
||||
Promise.race(Promise.new(function() end))
|
||||
end)
|
||||
|
||||
expect(ok).to.be.ok()
|
||||
expect(err:find("Non%-promise")).to.be.ok()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Promise.promisify", function()
|
||||
|
|
Loading…
Reference in a new issue