function Promise._all(F,Q,h)if type(Q)~="\116\097\098\108\101"then error(string.format(ERROR_NON_LIST,"\080\114\111\109\105\115\101\046\097\108\108"),3)end for F,Q in pairs(Q)do if not Promise.is(Q)then error(string.format(ERROR_NON_PROMISE_IN_LIST,"\080\114\111\109\105\115\101\046\097\108\108",tostring(F)),3)end end if#Q==0 or h==0 then return Promise.resolve({})end return Promise._new(F,function(F,q,z)local o={}local y={}local E=0 local Z=0 local c=false local function d()for F,Q in ipairs(y)do Q:cancel()end end local function w(q,...)if c then return end E=E+1 if h==nil then o[q]=...else o[E]=...end if E>=(h or#Q)then c=true F(o)d()end end z(d)for F,z in ipairs(Q)do y[F]=z:andThen(function(...)w(F,...)end,function(...)Z=Z+1 if h==nil or#Q-Z<h then d()c=true q(...)end end)end if c then d()end end)end function Promise.all(F)return Promise._all(debug.traceback(nil,2),F)end Promise.All=Promise.all function Promise.some(F,Q)assert(type(Q)=="\110\117\109\098\101\114","\066\097\100 \097\114\103\117\109\101\110\116 \035\050 \116\111 \080\114\111\109\105\115\101\046\115\111\109\101\058 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")return Promise._all(debug.traceback(nil,2),F,Q)end Promise.Some=Promise.some function Promise.any(F)return(Promise._all(debug.traceback(nil,2),F,1)):andThen(function(F)return F[1]end)end Promise.Any=Promise.any function Promise.allSettled(F)if type(F)~="\116\097\098\108\101"then error(string.format(ERROR_NON_LIST,"\080\114\111\109\105\115\101\046\097\108\108\083\101\116\116\108\101\100"),2)end for F,Q in pairs(F)do if not Promise.is(Q)then error(string.format(ERROR_NON_PROMISE_IN_LIST,"\080\114\111\109\105\115\101\046\097\108\108\083\101\116\116\108\101\100",tostring(F)),2)end end if#F==0 then return Promise.resolve({})end return Promise._new(debug.traceback(nil,2),function(Q,h,q)local z={}local o={}local y=0 local function E(h,...)y=y+1 z[h]=...if y>=#F then Q(z)end end q(function()for F,Q in ipairs(o)do Q:cancel()end end)for F,Q in ipairs(F)do o[F]=Q:finally(function(...)E(F,...)end)end end)end Promise.AllSettled=Promise.allSettled

function Promise._all(traceback, promises, amount)
	if type(promises) ~= "table" then
		error(string.format(ERROR_NON_LIST, "Promise.all"), 3)
	end

	-- We need to check that each value is a promise here so that we can produce
	-- a proper error rather than a rejected promise with our error.
	for i, promise in pairs(promises) do
		if not Promise.is(promise) then
			error(string.format(ERROR_NON_PROMISE_IN_LIST, "Promise.all", tostring(i)), 3)
		end
	end

	-- If there are no values then return an already resolved promise.
	if #promises == 0 or amount == 0 then
		return Promise.resolve({})
	end

	return Promise._new(traceback, function(resolve, reject, onCancel)
		-- An array to contain our resolved values from the given promises.
		local resolvedValues = {}
		local newPromises = {}

		-- Keep a count of resolved promises because just checking the resolved
		-- values length wouldn't account for promises that resolve with nil.
		local resolvedCount = 0
		local rejectedCount = 0
		local done = false

		local function cancel()
			for _, promise in ipairs(newPromises) do
				promise:cancel()
			end
		end

		-- Called when a single value is resolved and resolves if all are done.
		local function resolveOne(i, ...)
			if done then
				return
			end

			resolvedCount = resolvedCount + 1

			if amount == nil then
				resolvedValues[i] = ...
			else
				resolvedValues[resolvedCount] = ...
			end

			if resolvedCount >= (amount or #promises) then
				done = true
				resolve(resolvedValues)
				cancel()
			end
		end

		onCancel(cancel)

		-- We can assume the values inside `promises` are all promises since we
		-- checked above.
		for i, promise in ipairs(promises) do
			newPromises[i] = promise:andThen(
				function(...)
					resolveOne(i, ...)
				end,
				function(...)
					rejectedCount = rejectedCount + 1

					if amount == nil or #promises - rejectedCount < amount then
						cancel()
						done = true

						reject(...)
					end
				end
			)
		end

		if done then
			cancel()
		end
	end)
end

function Promise.all(promises)
	return Promise._all(debug.traceback(nil, 2), promises)
end
Promise.All = Promise.all

function Promise.some(promises, amount)
	assert(type(amount) == "number", "Bad argument #2 to Promise.some: must be a number")

	return Promise._all(debug.traceback(nil, 2), promises, amount)
end
Promise.Some = Promise.some

function Promise.any(promises)
	return Promise._all(debug.traceback(nil, 2), promises, 1):andThen(function(values)
		return values[1]
	end)
end
Promise.Any = Promise.any

function Promise.allSettled(promises)
	if type(promises) ~= "table" then
		error(string.format(ERROR_NON_LIST, "Promise.allSettled"), 2)
	end

	-- We need to check that each value is a promise here so that we can produce
	-- a proper error rather than a rejected promise with our error.
	for i, promise in pairs(promises) do
		if not Promise.is(promise) then
			error(string.format(ERROR_NON_PROMISE_IN_LIST, "Promise.allSettled", tostring(i)), 2)
		end
	end

	-- If there are no values then return an already resolved promise.
	if #promises == 0 then
		return Promise.resolve({})
	end

	return Promise._new(debug.traceback(nil, 2), function(resolve, _, onCancel)
		-- An array to contain our resolved values from the given promises.
		local fates = {}
		local newPromises = {}

		-- Keep a count of resolved promises because just checking the resolved
		-- values length wouldn't account for promises that resolve with nil.
		local finishedCount = 0

		-- Called when a single value is resolved and resolves if all are done.
		local function resolveOne(i, ...)
			finishedCount = finishedCount + 1

			fates[i] = ...

			if finishedCount >= #promises then
				resolve(fates)
			end
		end

		onCancel(function()
			for _, promise in ipairs(newPromises) do
				promise:cancel()
			end
		end)

		-- We can assume the values inside `promises` are all promises since we
		-- checked above.
		for i, promise in ipairs(promises) do
			newPromises[i] = promise:finally(
				function(...)
					resolveOne(i, ...)
				end
			)
		end
	end)
end
Promise.AllSettled = Promise.allSettled
