7

I wonder if the two following pieces of JavaScript code are equivalent:

Using await Promise.all:

[foo, bar] = await Promise.all([getFoo(), getBar()])

Using an array with await:

[foo, bar] = [
  await getFoo(),
  await getBar()
]
Steren
  • 7,311
  • 3
  • 31
  • 51
  • 4
    It would be cleaner to answer "yes" rather than to point to 3 questions that ask how to run promises in parallel. My question is about the equivalence of the syntax, not how to parallelize promises – Steren Nov 18 '17 at 21:59
  • 4
    Actually, reading the great MDN, it looks like the answer is "no": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Do_not_confuse_await_for_Promise.all – Steren Nov 18 '17 at 22:11
  • The note has been removed and replaced with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Do_not_confuse_await_for_Promisethen – Qwerty Jan 01 '19 at 20:17
  • The answer is... no, these are categorically not equivalent. There is a solid explanation of this [here](https://stackoverflow.com/questions/45285129/any-difference-between-await-promise-all-and-multiple-await/54291660#54291660). TLDR: the failure modes are different. – Ben Aston May 05 '20 at 15:47

0 Answers0