mirror of
				https://github.com/AmberGraceRblx/luau-promise.git
				synced 2025-11-04 03:59:17 +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)
 | 
			
		||||
- 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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								lib/init.lua
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
		Loading…
	
		Reference in a new issue