Search code examples
javascriptnode.jses6-promise

Promise.all return more values


I'm trying to make two async calls running concurrently and waiting until they are finished with Promise.all. How can I make Promise.all return more than one value using async/await in es6 in nodejs? I wish I could see the assigned variables outside. This code is wrong but represents what I need.

  Promise.all([
    var rowA = await buildRow(example, countries),
    var rowM = await buildRow(example, countries)
  ])
  console.log(rowA+rowB)

Is there a way to see those variables outside the scope?


Solution

  • How can I make Promise.all return more than one value using async/await in es6 in nodejs?

    Promise.all resolved value is always an array where each item is the resolved value of the individual promises passed to it. The results are in the same order as the promises.

      const [rowA, rowB] = await Promise.all([
            buildRow(example, countries),
            buildRow(example, countries)
      ]);
    
      console.log(rowA, rowB);
    

    Using destructuring we assign to rowA the first result, and rowB the second one.