mirror of
https://github.com/AmberGraceRblx/luau-promise.git
synced 2025-04-24 23:50:03 +00:00
Let Promise:expect() throw rejection objects
This commit is contained in:
parent
0c30e0c40b
commit
292e47293c
3 changed files with 34 additions and 5 deletions
|
@ -9,6 +9,7 @@
|
||||||
- Promise.async has been renamed to Promise.defer (Promise.async references same function for compatibility)
|
- Promise.async has been renamed to Promise.defer (Promise.async references same function for compatibility)
|
||||||
- Improve test coverage for asynchronous and time-driven functions
|
- 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.
|
- 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
|
# 2.5.1
|
||||||
|
|
||||||
|
|
10
lib/init.lua
10
lib/init.lua
|
@ -932,13 +932,13 @@ end
|
||||||
--[[
|
--[[
|
||||||
Calls awaitStatus internally, returns (isResolved, values...)
|
Calls awaitStatus internally, returns (isResolved, values...)
|
||||||
]]
|
]]
|
||||||
function Promise.prototype:await(...)
|
function Promise.prototype:await()
|
||||||
return awaitHelper(self:awaitStatus(...))
|
return awaitHelper(self:awaitStatus())
|
||||||
end
|
end
|
||||||
|
|
||||||
local function expectHelper(status, ...)
|
local function expectHelper(status, ...)
|
||||||
if status ~= Promise.Status.Resolved then
|
if status ~= Promise.Status.Resolved then
|
||||||
error((...) == nil and "" or tostring((...)), 3)
|
error((...) == nil and "Expected Promise rejected with no value." or (...), 3)
|
||||||
end
|
end
|
||||||
|
|
||||||
return ...
|
return ...
|
||||||
|
@ -948,8 +948,8 @@ end
|
||||||
Calls await and only returns if the Promise resolves.
|
Calls await and only returns if the Promise resolves.
|
||||||
Throws if the Promise rejects or gets cancelled.
|
Throws if the Promise rejects or gets cancelled.
|
||||||
]]
|
]]
|
||||||
function Promise.prototype:expect(...)
|
function Promise.prototype:expect()
|
||||||
return expectHelper(self:awaitStatus(...))
|
return expectHelper(self:awaitStatus())
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Backwards compatibility
|
-- Backwards compatibility
|
||||||
|
|
|
@ -1161,4 +1161,32 @@ return function()
|
||||||
expect(promises[3]:getStatus()).to.equal(Promise.Status.Started)
|
expect(promises[3]:getStatus()).to.equal(Promise.Status.Started)
|
||||||
end)
|
end)
|
||||||
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
|
end
|
Loading…
Reference in a new issue