简体   繁体   中英

How to wait for all promises to be resolved before continuing

I have this code

     async function concurrent() {
        await Promise.all([
            getColorCount('black'),
            getColorCount('purple'),
            getColorCount('red'),
            getColorCount('amber'),
            getColorCount('yellow'),
            getColorCount('white'),
            buildChartData()
        ]);

    }
    concurrent();
    res.json(chartData); //This is run before above promises are complete

My understanding was that async await halted the code until complete, but in this case, res.json(chartData) runs before the above promises have been completed.

How do I ensure that all promises have completed before responding to the client with res.json(chartData); ?

return a value from the function, or use .then() within concurrent() function call

 function concurrent() {
   return Promise.all([
        getColorCount('black'),
        getColorCount('purple'),
        getColorCount('red'),
        getColorCount('amber'),
        getColorCount('yellow'),
        getColorCount('white'),
        buildChartData()
    ]);

  }
  concurrent()
  .then(chartData => 
    res.json(chartData)
  )
  .catch(err => console.error(err));

 async function concurrent() {
   const promises = await Promise.all([
        getColorCount('black'),
        getColorCount('purple'),
        getColorCount('red'),
        getColorCount('amber'),
        getColorCount('yellow'),
        getColorCount('white'),
        buildChartData()
    ]);
    return promises;
  }
  concurrent()
  .then(chartData => 
    res.json(chartData)
  )
  .catch(err => console.error(err));

Use

concurrent().then(promiseResult => {
      res.json(chartData);
});

An async function always returns a promise and hence using "then", you can ensure the completion of an async function in a synchronised way.

You will need .then after promise.all. Then will return an array of values returned from your function calls in order they were initially called.

Promise.all([ getColorCount('black'), getColorCount('purple'), getColorCount('red'), getColorCount('amber'), getColorCount('yellow'), getColorCount('white'), buildChartData() ]) .then( function( responeses ) { console.log ( responses )};

And take a look at the responses array, it should all fall into place

concurrent is an asynchrounous function, as you indicate with the keyword async . That's why res.json is executed just after calling concurrent, without waiting.

You can be tempted to use await like this:

await concurrent();
res.json(chartData);

but it will throw an error because await can't be used outside a async context.

Unluckily you will need the traditional then to fulfill the promise:

 concurrent().then(() => {
     res.json(chartData);
 });

Apart from that, is buildChartData asynchronous? Maybe it should be executed outside Promimse.all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM