Why don't you use Promise.each or Promise.all ? It would be more understandable and flexible.
Please check the examples below.
var Promise = require('bluebird');
var someArray = ['foo', 'bar', 'baz', 'qux'];
Promise.all(someArray.map(function(singleArrayElement){
//do something and return
return doSomethingWithElement(singleArrayElement);
})).then(function(results){
//do something with results
});
Promise.each(someArray, function(singleArrayElement){
//do something and return
return doSomethingWithElement(singleArrayElement);
}).then(function(results){
//do something with results
});
Or you may have loop in loop. So just an example if you have array of arrays.
var Promise = require('bluebird');
var arrayOfArrays = [['foo', 'bar', 'baz', 'qux'],['foo', 'bar', 'baz', 'qux']];
function firstLoopPromise(singleArray){
return Promise.all(singleArray.map(function(signleArrayElement){
//do something and return
return doSomethingWithElement(signleArrayElement);
}));
}
Promise.all(arrayOfArrays.map(function(singleArray){
//do something and return
return firstLoopPromise(singleArray);
})).then(function(results){
//do something with results
});