Node.jsQuestion 10 of 26
Promise or async/await?
Answer out loud or in your head first, then compare.
Answer
async/await is syntax built on Promises, not a separate asynchronous mechanism. It makes sequences easier to read and lets you handle errors with try/catch.
JavaScript
const [profile, notifications] = await Promise.all([
getProfile(userId),
getNotifications(userId),
]);In practice: I use await for a readable flow, and Promise combinators to coordinate independent operations. Promise.all rejects on the first rejection; allSettled gives the status of every operation.
Pitfall: two awaits in a row needlessly add up the time if the operations could have started together. Rejection of Promise.all doesn't cancel the other operations by itself.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- B-P05 · How do async and await work? — JavaScript, TypeScript and Node.js