mirror of
				https://github.com/AmberGraceRblx/luau-promise.git
				synced 2025-10-31 02:19:17 +00:00 
			
		
		
		
	
							parent
							
								
									54e510d909
								
							
						
					
					
						commit
						34273a7efc
					
				
					 2 changed files with 39 additions and 2 deletions
				
			
		
							
								
								
									
										13
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								lib/init.lua
									
									
									
									
									
								
							|  | @ -233,6 +233,9 @@ function Promise._new(traceback, callback, parent) | ||||||
| 	end | 	end | ||||||
| 
 | 
 | ||||||
| 	local self = { | 	local self = { | ||||||
|  | 		-- The executor thread. | ||||||
|  | 		_thread = nil, | ||||||
|  | 
 | ||||||
| 		-- Used to locate where a promise was created | 		-- Used to locate where a promise was created | ||||||
| 		_source = traceback, | 		_source = traceback, | ||||||
| 
 | 
 | ||||||
|  | @ -292,13 +295,15 @@ function Promise._new(traceback, callback, parent) | ||||||
| 		return self._status == Promise.Status.Cancelled | 		return self._status == Promise.Status.Cancelled | ||||||
| 	end | 	end | ||||||
| 
 | 
 | ||||||
| 	coroutine.wrap(function() | 	self._thread = coroutine.create(function() | ||||||
| 		local ok, _, result = runExecutor(self._source, callback, resolve, reject, onCancel) | 		local ok, _, result = runExecutor(self._source, callback, resolve, reject, onCancel) | ||||||
| 
 | 
 | ||||||
| 		if not ok then | 		if not ok then | ||||||
| 			reject(result[1]) | 			reject(result[1]) | ||||||
| 		end | 		end | ||||||
| 	end)() | 	end) | ||||||
|  | 
 | ||||||
|  | 	task.spawn(self._thread) | ||||||
| 
 | 
 | ||||||
| 	return self | 	return self | ||||||
| end | end | ||||||
|  | @ -1385,6 +1390,8 @@ function Promise.prototype:cancel() | ||||||
| 		self._cancellationHook() | 		self._cancellationHook() | ||||||
| 	end | 	end | ||||||
| 
 | 
 | ||||||
|  | 	coroutine.close(self._thread) | ||||||
|  | 
 | ||||||
| 	if self._parent then | 	if self._parent then | ||||||
| 		self._parent:_consumerCancelled(self) | 		self._parent:_consumerCancelled(self) | ||||||
| 	end | 	end | ||||||
|  | @ -1847,6 +1854,8 @@ function Promise.prototype:_finalize() | ||||||
| 		self._parent = nil | 		self._parent = nil | ||||||
| 		self._consumers = nil | 		self._consumers = nil | ||||||
| 	end | 	end | ||||||
|  | 
 | ||||||
|  | 	task.defer(coroutine.close, self._thread) | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| --[=[ | --[=[ | ||||||
|  |  | ||||||
|  | @ -218,6 +218,20 @@ return function() | ||||||
| 
 | 
 | ||||||
| 			expect(called).to.equal(true) | 			expect(called).to.equal(true) | ||||||
| 		end) | 		end) | ||||||
|  | 
 | ||||||
|  | 		itSKIP("should close the thread after resolve", function() | ||||||
|  | 			local count = 0 | ||||||
|  | 			Promise.new(function(resolve) | ||||||
|  | 				count += 1 | ||||||
|  | 				resolve() | ||||||
|  | 				Promise.delay(1):await() | ||||||
|  | 				count += 1 | ||||||
|  | 			end) | ||||||
|  | 
 | ||||||
|  | 			task.wait(1) | ||||||
|  | 
 | ||||||
|  | 			expect(count).to.equal(1) | ||||||
|  | 		end) | ||||||
| 	end) | 	end) | ||||||
| 
 | 
 | ||||||
| 	describe("Promise.defer", function() | 	describe("Promise.defer", function() | ||||||
|  | @ -627,6 +641,20 @@ return function() | ||||||
| 			expect(p1._status).to.equal(Promise.Status.Cancelled) | 			expect(p1._status).to.equal(Promise.Status.Cancelled) | ||||||
| 			expect(p2._status).to.equal(Promise.Status.Cancelled) | 			expect(p2._status).to.equal(Promise.Status.Cancelled) | ||||||
| 		end) | 		end) | ||||||
|  | 
 | ||||||
|  | 		it("should close the promise thread", function() | ||||||
|  | 			local count = 0 | ||||||
|  | 			local promise = Promise.new(function() | ||||||
|  | 				count += 1 | ||||||
|  | 				Promise.delay(1):await() | ||||||
|  | 				count += 1 | ||||||
|  | 			end) | ||||||
|  | 
 | ||||||
|  | 			promise:cancel() | ||||||
|  | 			advanceTime(2) | ||||||
|  | 
 | ||||||
|  | 			expect(count).to.equal(1) | ||||||
|  | 		end) | ||||||
| 	end) | 	end) | ||||||
| 
 | 
 | ||||||
| 	describe("Promise:finally", function() | 	describe("Promise:finally", function() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue