search

Home  >  Q&A  >  body text

javascript - How to implement multiple promises nested in a loop?

The general situation is as follows:

First obtain an array (regions) from a request, ignore this step, it has been processed previously;
Traverse this array to obtain the required information. This allows N asynchronous requests to be sent.
Process the returned data after all these asynchronous requests are completed.

The problem I encountered here is that every time it seems to go directly to the outer then method, shouldn't all the inner thens be resolved before entering the outer then method? How should I rewrite my question?

var promises = [];
promises = regions.map(function (region) {
  return new Promise(function(resolve) {
    Promise.all([asyncRequest1(region), asyncRequest2(region), asyncRequest3(region), asyncRequest4(region)])
    .then(function (reses) {
      resolve(reses);
    });
  });
  
});

Promise.all([promises]).then(function(results) {
  handle(results);
});
漂亮男人漂亮男人2714 days ago1375

reply all(1)I'll reply

  • 代言

    代言2017-06-28 09:31:10

    Promise.all([promises]), promises are already arrays

    reply
    0
  • Cancelreply