do local s local u function Promise.delay(X)assert(type(X)=="\110\117\109\098\101\114","\066\097\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \080\114\111\109\105\115\101\046\100\101\108\097\121\044 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114\046")if not(X>=.016666666666667)or X==math.huge then X=.016666666666667 end return Promise._new(debug.traceback(nil,2),function(x,y,t)local W=Promise._getTime()local l=W+X local c={resolve=x;startTime=W;endTime=l}if u==nil then s=c u=Promise._timeEvent:Connect(function()local X=Promise._getTime()while s~=nil and s.endTime<X do local X=s s=X.next if s==nil then u:Disconnect()u=nil else s.previous=nil end X.resolve(Promise._getTime()-X.startTime)end end)else if s.endTime<l then local u=s local X=u.next while X~=nil and X.endTime<l do u=X X=u.next end u.next=c c.previous=u if X~=nil then c.next=X X.previous=c end else c.next=s s.previous=c s=c end end t(function()local X=c.next if s==c then if X==nil then u:Disconnect()u=nil else X.previous=nil end s=X else local s=c.previous s.next=X if X~=nil then X.previous=s end end end)end)end Promise.Delay=Promise.delay end

do
	-- uses a sorted doubly linked list (queue) to achieve O(1) remove operations and O(n) for insert

	-- the initial node in the linked list
	local first
	local connection

	function Promise.delay(seconds)
		assert(type(seconds) == "number", "Bad argument #1 to Promise.delay, must be a number.")
		-- If seconds is -INF, INF, NaN, or less than 1 / 60, assume seconds is 1 / 60.
		-- This mirrors the behavior of wait()
		if not (seconds >= 1 / 60) or seconds == math.huge then
			seconds = 1 / 60
		end

		return Promise._new(debug.traceback(nil, 2), function(resolve, _, onCancel)
			local startTime = Promise._getTime()
			local endTime = startTime + seconds

			local node = {
				resolve = resolve,
				startTime = startTime,
				endTime = endTime,
			}

			if connection == nil then -- first is nil when connection is nil
				first = node
				connection = Promise._timeEvent:Connect(function()
					local threadStart = Promise._getTime()

					while first ~= nil and first.endTime < threadStart do
						local current = first
						first = current.next

						if first == nil then
							connection:Disconnect()
							connection = nil
						else
							first.previous = nil
						end

						current.resolve(Promise._getTime() - current.startTime)
					end
				end)
			else -- first is non-nil
				if first.endTime < endTime then -- if `node` should be placed after `first`
					-- we will insert `node` between `current` and `next`
					-- (i.e. after `current` if `next` is nil)
					local current = first
					local next = current.next

					while next ~= nil and next.endTime < endTime do
						current = next
						next = current.next
					end

					-- `current` must be non-nil, but `next` could be `nil` (i.e. last item in list)
					current.next = node
					node.previous = current

					if next ~= nil then
						node.next = next
						next.previous = node
					end
				else
					-- set `node` to `first`
					node.next = first
					first.previous = node
					first = node
				end
			end

			onCancel(function()
				-- remove node from queue
				local next = node.next

				if first == node then
					if next == nil then -- if `node` is the first and last
						connection:Disconnect()
						connection = nil
					else -- if `node` is `first` and not the last
						next.previous = nil
					end
					first = next
				else
					local previous = node.previous
					-- since `node` is not `first`, then we know `previous` is non-nil
					previous.next = next

					if next ~= nil then
						next.previous = previous
					end
				end
			end)
		end)
	end
	Promise.Delay = Promise.delay
end
