0

Currently, I'm creating and assigning values to a few variables at once, as the result of corresponding asynchronous functions resolving as such:

const [first, second] = await Promise.all([asynFunc(),asynFunc()])

Then, I'm creating an array with first and second:

const res = [first, second]

Is there a way to combine the steps to both create res as well as create and assign first and second?

yalpsid eman
  • 3,064
  • 6
  • 45
  • 71

3 Answers3

0

You can get array at the first step

const res = await Promise.all([asynFunc(),asynFunc()])
lfermincas
  • 35
  • 3
0
const res =  [first, second] = await Promise.all([asynFunc(),asynFunc()])
Jacob Penney
  • 725
  • 4
  • 9
  • 1
    That does create *global* variables `first` and `second` and will not work in strict mode – Bergi Nov 26 '19 at 22:06
0

You're probably looking for

const res = await Promise.all([asynFunc(),asynFunc()]),
      [first, second] = res;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375