do local R local w function Promise.delay(e)assert(type(e)=="\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(e>=.016666666666667)or e==math.huge then e=.016666666666667 end return Promise._new(debug.traceback(nil,2),function(Q,S,D)local g=Promise._getTime()local Z=g+e local E={resolve=Q,startTime=g,endTime=Z}if w==nil then R=E w=Promise._timeEvent:Connect(function()local e=Promise._getTime()while R~=nil and R.endTime<e do local e=R R=e.next if R==nil then w:Disconnect()w=nil else R.previous=nil end e.resolve(Promise._getTime()-e.startTime)end end)else if R.endTime<Z then local w=R local e=w.next while e~=nil and e.endTime<Z do w=e e=w.next end w.next=E E.previous=w if e~=nil then E.next=e e.previous=E end else E.next=R R.previous=E R=E end end D(function()local e=E.next if R==E then if e==nil then w:Disconnect()w=nil else e.previous=nil end R=e else local R=E.previous R.next=e if e~=nil then e.previous=R 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
