2

I'm trying to see myself the different between Promise.all and await Promise.all. I have learnt that the first one ends earlier if one of the promises fail but in the same case with await we have to wait to the completion of all promises.

In my example, in both cases, they finish at the same time. What I'm doing wrong?

/**
 * Create a promise
 * @param ok true -> resolve, false -> reject
 */
function create_promise(ok) {
  return new Promise((resolve, reject) => {
    setTimeout(() => ok ? resolve() : reject(), 2e3)
  })
}

// Finish as soon as I get a reject
Promise.all([create_promise(true), create_promise(false), create_promise(true)])
  .catch(() => console.log('rejected'))

// Finish after waiting all
const my_async_function = async () => 
  await Promise.all([create_promise(true), create_promise(false), create_promise(true)])

my_async_function().catch(() => console.log('rejected'))
Johnny Johnny
  • 319
  • 2
  • 11
  • Are you trying to time them? `Promise.all` is going to run all of the Promises at the same time in any case. I'm not sure I understand your goal – igg Jun 01 '20 at 11:59
  • Why wouldn't they finish at the same time? All promises resolve at the same time in 2 seconds. What do you expect to happen instead? – VLAZ Jun 01 '20 at 12:14
  • 1
    `Promise.all` rejects when just one of promises reject. I don't think awaiting makes any difference here. According to [this answer](https://stackoverflow.com/questions/30362733/handling-errors-in-promise-all) it was an old API `Promise.when` that waited till all promises resolve or reject. – Wiktor Zychla Jun 01 '20 at 12:20
  • I'm making on propose that second promise fails to see how `Promise.all` without `await` finishes earlier – Johnny Johnny Jun 01 '20 at 12:22
  • 2
    "*I have learnt …*" - where have you learnt that? No, you're using `Promise.all` in both cases, and it will behave the same in both situations. It doesn't matter whether you use `.then()` or `await` on the resulting promise. – Bergi Jun 01 '20 at 14:05
  • Thank you so much @Bergi, so `Promise.all` with `.then()` and `await` it's the same : ) – Johnny Johnny Jun 02 '20 at 00:17

1 Answers1

3

await Promise.all will ensure that all the promises will be resolved before executing the line after the await.

/**
 * Create a promise
 * @param ok true -> resolve, false -> reject
 */
function create_promise(ok) {
  return new Promise((resolve, reject) => {
    setTimeout(() => ok ? resolve() : reject(), 2e3)
  })
}

console.log("async/await version...")
const my_async_function = async () => {
  console.log("before await promise.all");
  const res = await Promise.all([create_promise(true), create_promise(false), create_promise(true)]);
  console.log("after await  promise.all"); // This line will be executed only after all promises are successfully resolved
  return res;
  }
my_async_function().catch(() => console.log('rejected'))

console.log("without async/await version...")
const my_SYNC_function = () => {
  console.log("before promise.all");
  const res = Promise.all([create_promise(true), create_promise(false), create_promise(true)]);
  console.log("after promise.all"); // This line will always be immediately executed after the Promise.all line, and not waiting for the promise completion  at all.
  return res; // So this line returns the return value of Promise.all, which will be an unresolved promise because the 2 seconds have not been elapsed yet.
  }
  
my_SYNC_function().catch(() => console.log('rejected'))

This has nothing to do with a Promise itself being somehow "executed faster".

However, the function that calls Promise.all without await returns earlier, it just returns some Promise that has not been resolved yet.

Pac0
  • 21,465
  • 8
  • 65
  • 74