mirror of
				https://github.com/AmberGraceRblx/luau-promise.git
				synced 2025-11-02 19:29:19 +00:00 
			
		
		
		
	Promise.delay fix
This commit is contained in:
		
							parent
							
								
									06066c2dd0
								
							
						
					
					
						commit
						d1ff9be774
					
				
					 1 changed files with 16 additions and 18 deletions
				
			
		
							
								
								
									
										34
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								lib/init.lua
									
									
									
									
									
								
							| 
						 | 
					@ -5,11 +5,10 @@
 | 
				
			||||||
local ERROR_NON_PROMISE_IN_LIST = "Non-promise value passed into %s at index %s"
 | 
					local ERROR_NON_PROMISE_IN_LIST = "Non-promise value passed into %s at index %s"
 | 
				
			||||||
local ERROR_NON_LIST = "Please pass a list of promises to %s"
 | 
					local ERROR_NON_LIST = "Please pass a list of promises to %s"
 | 
				
			||||||
local ERROR_NON_FUNCTION = "Please pass a handler function to %s!"
 | 
					local ERROR_NON_FUNCTION = "Please pass a handler function to %s!"
 | 
				
			||||||
 | 
					local TIME_GETTER = os.clock
 | 
				
			||||||
 | 
					local TIME_EVENT = game:GetService("RunService").Heartbeat
 | 
				
			||||||
local MODE_KEY_METATABLE = {__mode = "k"}
 | 
					local MODE_KEY_METATABLE = {__mode = "k"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local RunService = game:GetService("RunService")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
--[[
 | 
					--[[
 | 
				
			||||||
	Creates an enum dictionary with some metamethods to prevent common mistakes.
 | 
						Creates an enum dictionary with some metamethods to prevent common mistakes.
 | 
				
			||||||
]]
 | 
					]]
 | 
				
			||||||
| 
						 | 
					@ -54,7 +53,7 @@ local Error do
 | 
				
			||||||
			context = options.context,
 | 
								context = options.context,
 | 
				
			||||||
			kind = options.kind,
 | 
								kind = options.kind,
 | 
				
			||||||
			parent = parent,
 | 
								parent = parent,
 | 
				
			||||||
			createdTick = tick(),
 | 
								createdTick = TIME_GETTER(),
 | 
				
			||||||
			createdTrace = debug.traceback(),
 | 
								createdTrace = debug.traceback(),
 | 
				
			||||||
		}, Error)
 | 
							}, Error)
 | 
				
			||||||
	end
 | 
						end
 | 
				
			||||||
| 
						 | 
					@ -178,8 +177,6 @@ end
 | 
				
			||||||
local Promise = {
 | 
					local Promise = {
 | 
				
			||||||
	Error = Error,
 | 
						Error = Error,
 | 
				
			||||||
	Status = makeEnum("Promise.Status", {"Started", "Resolved", "Rejected", "Cancelled"}),
 | 
						Status = makeEnum("Promise.Status", {"Started", "Resolved", "Rejected", "Cancelled"}),
 | 
				
			||||||
	_timeEvent = RunService.Heartbeat,
 | 
					 | 
				
			||||||
	_getTime = tick,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
Promise.prototype = {}
 | 
					Promise.prototype = {}
 | 
				
			||||||
Promise.__index = Promise.prototype
 | 
					Promise.__index = Promise.prototype
 | 
				
			||||||
| 
						 | 
					@ -294,7 +291,7 @@ function Promise.defer(callback)
 | 
				
			||||||
	local promise
 | 
						local promise
 | 
				
			||||||
	promise = Promise._new(traceback, function(resolve, reject, onCancel)
 | 
						promise = Promise._new(traceback, function(resolve, reject, onCancel)
 | 
				
			||||||
		local connection
 | 
							local connection
 | 
				
			||||||
		connection = Promise._timeEvent:Connect(function()
 | 
							connection = TIME_EVENT:Connect(function()
 | 
				
			||||||
			connection:Disconnect()
 | 
								connection:Disconnect()
 | 
				
			||||||
			local ok, _, result = runExecutor(traceback, callback, resolve, reject, onCancel)
 | 
								local ok, _, result = runExecutor(traceback, callback, resolve, reject, onCancel)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -706,7 +703,7 @@ do
 | 
				
			||||||
		end
 | 
							end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return Promise._new(debug.traceback(nil, 2), function(resolve, _, onCancel)
 | 
							return Promise._new(debug.traceback(nil, 2), function(resolve, _, onCancel)
 | 
				
			||||||
			local startTime = Promise._getTime()
 | 
								local startTime = TIME_GETTER()
 | 
				
			||||||
			local endTime = startTime + seconds
 | 
								local endTime = startTime + seconds
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			local node = {
 | 
								local node = {
 | 
				
			||||||
| 
						 | 
					@ -717,19 +714,20 @@ do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if connection == nil then -- first is nil when connection is nil
 | 
								if connection == nil then -- first is nil when connection is nil
 | 
				
			||||||
				first = node
 | 
									first = node
 | 
				
			||||||
				connection = Promise._timeEvent:Connect(function()
 | 
									connection = TIME_EVENT:Connect(function()
 | 
				
			||||||
					local currentTime = Promise._getTime()
 | 
										while first.endTime <= TIME_GETTER() do
 | 
				
			||||||
					while first.endTime <= currentTime do
 | 
											local current = first
 | 
				
			||||||
						-- Don't use currentTime here, as this is the time when we started resolving,
 | 
											first = current.next
 | 
				
			||||||
						-- not necessarily the time *right now*.
 | 
					
 | 
				
			||||||
						first.resolve(Promise._getTime() - first.startTime)
 | 
					 | 
				
			||||||
						first = first.next
 | 
					 | 
				
			||||||
						if first == nil then
 | 
											if first == nil then
 | 
				
			||||||
							connection:Disconnect()
 | 
												connection:Disconnect()
 | 
				
			||||||
							connection = nil
 | 
												connection = nil
 | 
				
			||||||
							break
 | 
											else
 | 
				
			||||||
 | 
												first.previous = nil
 | 
				
			||||||
						end
 | 
											end
 | 
				
			||||||
						first.previous = nil
 | 
					
 | 
				
			||||||
 | 
											current.resolve(TIME_GETTER() - current.startTime)
 | 
				
			||||||
 | 
											if current.next == nil then return end -- kill this thread if there was no `first` before `resolve`
 | 
				
			||||||
					end
 | 
										end
 | 
				
			||||||
				end)
 | 
									end)
 | 
				
			||||||
			else -- first is non-nil
 | 
								else -- first is non-nil
 | 
				
			||||||
| 
						 | 
					@ -1250,7 +1248,7 @@ function Promise.prototype:_reject(...)
 | 
				
			||||||
		local err = tostring((...))
 | 
							local err = tostring((...))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		coroutine.wrap(function()
 | 
							coroutine.wrap(function()
 | 
				
			||||||
			Promise._timeEvent:Wait()
 | 
								TIME_EVENT:Wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			-- Someone observed the error, hooray!
 | 
								-- Someone observed the error, hooray!
 | 
				
			||||||
			if not self._unhandledRejection then
 | 
								if not self._unhandledRejection then
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue