local g=require((game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):WaitForChild("\078\101\118\101\114\109\111\114\101"))local C=g("\080\114\111\109\105\115\101")local Q={}function Q.race(g)local Q=C.new()local function D(g)return function(...)Q[g](Q,...)end end for g,C in pairs(g)do C:Then(D("\082\101\115\111\108\118\101"),D("\082\101\106\101\099\116"))end return Q end function Q.all(g)local Q=#g local D=C.new()local q={}local S=true local function W(C,W)return function(z)S=S and W q[C]=z Q=Q-1 if Q==0 then local C=S and"\082\101\115\111\108\118\101"or"\082\101\106\101\099\116"D[C](D,unpack(q,1,#g))end end end for g,C in pairs(g)do C:Then(W(g,true),W(g,false))end if#g==0 then D:Resolve()end return D end return Q
--- Utility methods for promise
-- @module PromiseUtils

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local Promise = require("Promise")

local lib = {}

--- Returns the value of the first promise resolved
-- @constructor First
-- @tparam Array(Promise) promises
-- @treturn Promise Promise that resolves with first result
function lib.race(promises)
	local returnPromise = Promise.new()

	local function syncronize(method)
		return function(...)
			returnPromise[method](returnPromise, ...)
		end
	end

	for _, promise in pairs(promises) do
		promise:Then(syncronize("Resolve"), syncronize("Reject"))
	end

	return returnPromise
end

--- Executes all promises. If any fails, the result will be rejected. However, it yields until
--  every promise is complete
-- @constructor First
-- @treturn Promise
function lib.all(promises)
	local remainingCount = #promises
	local returnPromise = Promise.new()
	local results = {}
	local allFulfilled = true

	local function syncronize(index, isFullfilled)
		return function(value)
			allFulfilled = allFulfilled and isFullfilled
			results[index] = value
			remainingCount = remainingCount - 1
			if remainingCount == 0 then
				local method = allFulfilled and "Resolve" or "Reject"
				returnPromise[method](returnPromise, unpack(results, 1, #promises))
			end
		end
	end

	for index, promise in pairs(promises) do
		promise:Then(syncronize(index, true), syncronize(index, false))
	end

	if #promises == 0 then
		returnPromise:Resolve()
	end

	return returnPromise
end

return lib