Back
JavaScriptMediumNew
How do async and await work under the hood?
async functions always return a Promise; await pauses via microtasks, not threads.
Ideal Answer
async / await is syntactic sugar over Promises. It does not create new threads — it makes asynchronous code read like synchronous code while still being non-blocking.
JavaScript
async function loadUser(id) {
try {
const res = await fetch(`/api/users/${id}`)
if (!res.ok) throw new Error(res.statusText)
return await res.json()
} catch (err) {
console.error(err)
throw err // re-throw to reject the returned Promise
}
}
Under the hood
- An
async functionalways returns a Promise — even if youreturn 42, callers getPromise.resolve(42). await expressionpauses the async function until the Promise settles.- The continuation after
awaitis scheduled as a microtask, not a new thread. - Errors thrown inside
asyncreject the returned Promise; usetry/catchfor local handling.
Parallel vs sequential
JavaScript
// Sequential — slower
const a = await fetchA()
const b = await fetchB()
// Parallel — start both, await together
const [a, b] = await Promise.all([fetchA(), fetchB()])
Interview tip: await only "blocks" the current async function, never the main thread or call stack.