do local a local D function Promise.delay(r)assert(type(r)=="\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(r>=.016666666666667)or r==math.huge then r=.016666666666667 end return Promise._new(debug.traceback(nil,2),function(s,Y,N)local o=Promise._getTime()local P=o+r local T={resolve=s;startTime=o,endTime=P}if D==nil then a=T D=Promise._timeEvent:Connect(function()local r=Promise._getTime()while a~=nil and a.endTime<r do local r=a a=r.next if a==nil then D:Disconnect()D=nil else a.previous=nil end r.resolve(Promise._getTime()-r.startTime)end end)else if a.endTime<P then local D=a local r=D.next while r~=nil and r.endTime<P do D=r r=D.next end D.next=T T.previous=D if r~=nil then T.next=r r.previous=T end else T.next=a a.previous=T a=T end end N(function()local r=T.next if a==T then if r==nil then D:Disconnect()D=nil else r.previous=nil end a=r else local a=T.previous a.next=r if r~=nil then r.previous=a end end end)end)end 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
end
