What is the best way to get result from a promise inside a For loop. In this example code resultArray is not complete when the loops ends.
var resultArr = [];
var itemArray1 = [1, 2, 3, 4, 5];
var itemArray2 = ['a','b','c','d','e'];
for (var a = 0; a < itemArray1.length; a++) {
for (var b = 0; b < itemArray2.length; b++) {
myPromise(a,b)
.then(function(result) {
if (result != null) {
resultArr.push(result);
}
});
}
}
// resultArray is still not complete
function myPromise(a,b) {
return new Promise(function(resolve, reject) {
// request to mongodb
myTable.findOne({ _id:a, name:b }, function(err,result) {
if (err) throw err;
resolve(result);
});
});
}
Promise.eachorPromise.map