1

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);
         });
     });
 } 
2
  • You can use for-each loop.. Commented Apr 20, 2016 at 6:22
  • Use Promise.each or Promise.map Commented Apr 20, 2016 at 6:34

2 Answers 2

1

In my opinion, the cleanest way to do things like this is to use Promise.all() with Array#map. Also, make sure to give keep your functions clean and concise, and give them meaningful names!

var itemArray1 = [1, 2, 3, 4, 5];
var itemArray2 = ['a','b','c','d','e'];

function flatten(arrays) {
    return [].concat(arrays);
}

function queryAs() {
    return Promise.all(itemArray1.map(queryBs))
        // the result is an array of arrays, so we'll flatten them here
        .then(flatten);
}

function queryBs(a) {
    return Promise.all(itemArray2.map(function (b) {
        return performQuery(a, b);
    }));
}

// resultArray is still not complete
function performQuery(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);
        });
    });
}

queryAs().then(function (results) {
    console.log(results);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. This was really a very clean way to do it!
1

You could use a combination of forEach and Promise.all

var resultArr = [];
var itemArray1 = [1, 2, 3, 4, 5];
var itemArray2 = ['a', 'b', 'c', 'd', 'e'];

var myPromise = (item1, item2) => new Promise((resolve, reject) => {
    myTable.findOne({
        _id: item1,
        name: item2
    }, function(err, result) {
        if (err) reject(err);
        else resolve(result);
    });
});

var promises = [];
itemArray1.forEach(item1 => {
    itemArray2.forEach(item2 => {
        promises.push(myPromise(item1, item2));
    });
});

Promise.all(promises).then(result => {
    console.log(result);
}).catch(err => {
    console.error(err.message);
});

1 Comment

Thank you for your answer. This was helpful too, also if I did not use this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.