Let Promise:expect() throw rejection objects

This commit is contained in:
Eryn Lynn 2020-05-06 14:20:34 -04:00
parent 0c30e0c40b
commit 292e47293c
3 changed files with 34 additions and 5 deletions

View file

@ -9,6 +9,7 @@
- Promise.async has been renamed to Promise.defer (Promise.async references same function for compatibility)
- Improve test coverage for asynchronous and time-driven functions
- Change Promise.is to be safe when dealing with tables that have an `__index` metamethod that creates an error.
- Let Promise:expect() throw rejection objects
# 2.5.1

View file

@ -932,13 +932,13 @@ end
--[[
Calls awaitStatus internally, returns (isResolved, values...)
]]
function Promise.prototype:await(...)
return awaitHelper(self:awaitStatus(...))
function Promise.prototype:await()
return awaitHelper(self:awaitStatus())
end
local function expectHelper(status, ...)
if status ~= Promise.Status.Resolved then
error((...) == nil and "" or tostring((...)), 3)
error((...) == nil and "Expected Promise rejected with no value." or (...), 3)
end
return ...
@ -948,8 +948,8 @@ end
Calls await and only returns if the Promise resolves.
Throws if the Promise rejects or gets cancelled.
]]
function Promise.prototype:expect(...)
return expectHelper(self:awaitStatus(...))
function Promise.prototype:expect()
return expectHelper(self:awaitStatus())
end
-- Backwards compatibility

View file

@ -1161,4 +1161,32 @@ return function()
expect(promises[3]:getStatus()).to.equal(Promise.Status.Started)
end)
end)
describe("Promise:await", function()
it("should return the correct values", function()
local promise = Promise.resolve(5, 6, nil, 7)
local a, b, c, d, e = promise:await()
expect(a).to.equal(true)
expect(b).to.equal(5)
expect(c).to.equal(6)
expect(d).to.equal(nil)
expect(e).to.equal(7)
end)
end)
describe("Promise:expect", function()
it("should throw the correct values", function()
local rejectionValue = {}
local promise = Promise.reject(rejectionValue)
local success, value = pcall(function()
promise:expect()
end)
expect(success).to.equal(false)
expect(value).to.equal(rejectionValue)
end)
end)
end