How to solve this async problem ? (using node.js)
A promise is iterating through a list, and multiple async functions are called with each iteration. How do I know when all async functions are finished, so that I can resolve the promise ?
All I found yet is to use a setTimeout()
which isn't efficient at all.
Here is a simplified sample of my code :
function myFunction(parameter){
return new Promise(function (resolve, reject) {
var resultToReturn = []
_.each(list, function(el){
doSomethingAsync(el, function(result){
resultToReturn.push(result)
});
doSomethingElseAsync(el, function(result){
resultToReturn.push(result)
});
});
// How to call this ⬋ only when all async functions are finished ?
resolve(resultToReturn)
});
}
myFunction(IAmAParameter).then(function(resultFromPromise){
console.log(resultFromPromise);
});
I know this kind of question was already asked many times, but answers were too specific and not relevant in this case.
Ex. :