Intermediate Node.js Interview QuestionsIntermediateConcept
Node.js · Question 60
How does async/await work in Node.js?
Direct answer
async functions return Promises; await pauses within the function until a Promise settles — syntax sugar over .then; use try/catch for errors; await serial I/O, Promise.all for independent parallel work; never block the event loop with CPU work inside async.
async/await makes Promise chains readable in route handlers and scripts — but each await still yields to the event loop; it does not create new threads.
Why interviewers ask
| Pattern | When |
|---|---|
| await step1(); await step2(); | Sequential dependencies |
| await Promise.all([a(), b()]) | Independent parallel I/O |
| for (const x of items) await fn(x) | Serial per item — safe for rate limits |
| Promise.allSettled | Batch where partial failure OK |
| top-level await | ESM scripts — import runners, CLI tools |
- try/catch — catches rejected await; map to HTTP 4xx/5xx in handlers.
- Floating promises — async IIFE without await at call site → unhandled rejection.
- Fastify handlers — async (req, reply) => { await service... } — framework catches rejections.
async-await.mjs
async function importTopics(slugs) {
const results = await Promise.all(
slugs.map(async (slug) => {
const topic = await loadTopic(slug);
await upsertTopic(topic);
return slug;
})
);
return results;
}
try {
await importTopics(["javascript-interview-questions", "nodejs-interview-questions"]);
} catch (err) {
console.error("Import failed:", err.message);
}