DEV Community

Discussion on: Awaiting for async promises in JavaScript

Collapse
 
mapleleaf profile image
MapleLeaf • Edited

Some advice: if multiple operations don't depend on the result of others, and especially if they might take a while, doing them concurrently is ideal.

async function bar() {
  const [a, b] = await Promise.all([
    someFunction(),
    someOtherFunction(),
  ])
  return a + b
}

This is a pitfall a lot of people tend to miss when using async/await; thought I should mention it here. Additional info for Promise.all

Collapse
 
rhymes profile image
rhymes • Edited

Thank you Darius!

It's worth noting that if any of the promises rejects the entire Promise.all() will be rejected, which is fine if you have to sum two numbers BUT it might not be the case if you are building a crawler (for example).

Maybe one's intention is "fire off these 10 operations and tell me which ones failed and which ones didn't" which I don't think you can do with the ES6 API easily.

Twisted has DeferredList which allows the user to fire off N deferred/promises and then attach a callback to the aggregate so you can iterate on the results and check which one succeeded and which one failed.

Collapse
 
mapleleaf profile image
MapleLeaf

Good point, didn't think about that. 👍