Interview frequency: …
Short answer
async/await is just syntactic sugar built on top of promises that makes code look like synchronous code, allows error handling with try/catch, and improves the readability of stack traces.
Explanation
The essential advantage of async/await lies in ease of reading and preventing nested chained then/catch, unifying synchronous and asynchronous error handling in a single try/catch block, in addition to enabling async stack traces.
Code example
try {
const user = await getUser(id);
} catch (err) {
handleError(err);
}Common mistakes
- Forgetting to add the await keyword, which returns a promise object instead of the required value and produces silent errors.
Follow-up questions
- How do errors travel in asynchronous code and what is the danger of errors inside traditional callbacks?
- What is the top-level await feature in ES modules in Node.js and what are the cautions of using it?