function Promise.fold(V,n,v)assert(type(V)=="\116\097\098\108\101","\066\097\100 \097\114\103\117\109\101\110\116 \035\049 \116\111 \080\114\111\109\105\115\101\046\102\111\108\100\058 \109\117\115\116 \098\101 \097 \116\097\098\108\101")assert(isCallable(n),"\066\097\100 \097\114\103\117\109\101\110\116 \035\050 \116\111 \080\114\111\109\105\115\101\046\102\111\108\100\058 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110")local S=Promise.resolve(v)return(Promise.each(V,function(V,v)S=S:andThen(function(S)return n(S,V,v)end)end)):andThen(function()return S end)end

function Promise.fold(list, reducer, initialValue)
	assert(type(list) == "table", "Bad argument #1 to Promise.fold: must be a table")
	assert(isCallable(reducer), "Bad argument #2 to Promise.fold: must be a function")

	local accumulator = Promise.resolve(initialValue)
	return Promise.each(list, function(resolvedElement, i)
		accumulator = accumulator:andThen(function(previousValueResolved)
			return reducer(previousValueResolved, resolvedElement, i)
		end)
	end):andThen(function()
		return accumulator
	end)
end
