-So i’m learning about Promise module and keep getting confused when learn new things,there are 2 functions that i want to understand:
1.Promise.fold()
2.Promise.race()
What are these functions do?
-I keep trying to ask people on discord,but there are no replies yet.
-I keep trying to understand it,but i can’t.
–//Promise.fold()\–
local basket = {"blueberry", "melon", "pear", "melon"}
Promise.fold(basket, function(cost, fruit)
if fruit == "blueberry" then
return cost -- blueberries are free!
else
-- call a function that returns a promise with the fruit price
return fetchPrice(fruit):andThen(function(fruitCost)
return cost + fruitCost
end)
end
end, 0)
–//Promise.race()\
local function async1()
return Promise.async(function(resolve, reject)
local ok, value = async2():await()
if not ok then
return reject(value)
end
resolve(value + 1)
end)
end
Please explain me about these two functions,Thanks for reading this post!