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
  • 2,198
  • 5
  • 29
  • 59

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
  • 694
  • 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
  • 572,313
  • 128
  • 898
  • 1,281