0

Consider the following:

async makesomething () {
  // These first calls should all be done concurrent...
  call1()
  call2()
  call3()
  // But I want to wait all the previous call to finish before calling call4()
  call4()
}

Adding an await before call1-2() will make the next call wait for it to finish, which is not the desired behavior... And if I add it on call3, call4 might happen before call1-2 finishes which again is not the desired behavior here... So how do I make call4 wait for the previous calls to finish without blocking any of the previous calls?

  • 2
    `Promise.all` ! – Bergi Feb 10 '22 at 19:49
  • 1
    You can use [Promise.all - doc from MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). `await Promise.all([call1(), call2(), call3()])` – Bao Huynh Lam Feb 10 '22 at 19:53
  • Does this answer your question? [How run async / await in parallel in Javascript](https://stackoverflow.com/questions/42158853/how-run-async-await-in-parallel-in-javascript) – Heretic Monkey Feb 10 '22 at 20:01
  • While these work seamlessly on this simple example, on my real code each call represents a very hairy block of code, which has multiple functions, conditions, paths, etc..I could in theory wrap them on a function call, but for whatever reasons, I would rather not. Is this the only way? There is no simple semaphore that I could just put before call4() and not have to change any other part of my code? – user3919706 Feb 10 '22 at 20:04

0 Answers0