<vetted />
JavaScript
Junior
Question 9 of 10

What makes async/await easier to work with than plain Promises?

Quick Answer

Async/await is syntactic sugar over Promises that makes asynchronous code look and behave more like synchronous code.

Detailed Answer6 paragraphs

Async/await, introduced in ES2017, provides a more intuitive way to write asynchronous code. It's built on top of Promises—understanding promises is essential to understanding async/await.

An async function always returns a Promise. If you return a non-promise value, it's automatically wrapped in a resolved promise. If you throw an error, it returns a rejected promise.

The await keyword can only be used inside an async function (or at the top level of a module). It pauses the execution of the async function until the awaited promise settles, then returns the resolved value. This makes async code read like synchronous code.

Error handling uses try/catch blocks instead of .catch(): async function getData() { try { const response = await fetch(url); const data = await response.json(); return data; } catch (error) { console.error('Failed:', error); throw error; } }

Multiple awaits run sequentially. For parallel execution, use Promise.all(): const [users, posts] = await Promise.all([fetchUsers(), fetchPosts()])

Common pitfalls include: forgetting that async functions return promises (you need to await them too), using await in loops when parallel execution would be more efficient, and not handling errors properly. Remember that await doesn't block the main thread—it only pauses that specific async function while letting other code run.

Key Takeaway

Async/await is syntactic sugar over Promises that makes asynchronous code look and behave more like synchronous code.

Ace your interview

Ready to Land Your Dream Job?

Join our network of elite AI-native engineers.