function Promise.race(L)assert(type(L)=="\116\097\098\108\101","\080\108\101\097\115\101 \112\097\115\115 \097 \108\105\115\116 \111\102 \112\114\111\109\105\115\101\115 \116\111 \080\114\111\109\105\115\101\046\114\097\099\101")for L,t in ipairs(L)do assert(Promise.is(t),("\078\111\110-\112\114\111\109\105\115\101 \118\097\108\117\101 \112\097\115\115\101\100 \105\110\116\111 \080\114\111\109\105\115\101\046\114\097\099\101 \097\116 \105\110\100\101\120 \035\037\100"):format(L))end return Promise.new(function(t,q,d)local function i(t)return function(...)for L,t in ipairs(L)do t:cancel()end return t(...)end end d(i(q))for L,d in ipairs(L)do d:andThen(i(t),i(q))end end)end

function Promise.race(promises)
	assert(type(promises) == "table", "Please pass a list of promises to Promise.race")

	for i, promise in ipairs(promises) do
		assert(Promise.is(promise), ("Non-promise value passed into Promise.race at index #%d"):format(i))
	end

	return Promise.new(function(resolve, reject, onCancel)
		local function finalize(callback)
			return function (...)
				for _, promise in ipairs(promises) do
					promise:cancel()
				end

				return callback(...)
			end
		end

		onCancel(finalize(reject))

		for _, promise in ipairs(promises) do
			promise:andThen(finalize(resolve), finalize(reject))
		end
	end)
end
