I'm working in node and must make n amount of API calls after getting n returned from a promise. My initial thought would be to create a foreach loop that creates n promises, store them in an array, and then call
Promise.all(promisesArray).then(function(){...});
However, this would have the be wrapped in the then()
call from the original promise, effectively setting up a nested promise situation.
getN() //returns a promise object.
.then(function(n) {
var promisesArray = [];
for(var i = 0, i < n; i++) {
var promise = new Promise(function(resolve, reject) { callSomething(i, resolve) });
promisesArray.push(promise);
}
Promises.all(promisesArray).then(function(){
...
resolve; //resolves original promise
});
.then(function(something) {...})
.catch(function(err) {...};
How can I avoid this? Thanks for your help!