- Home
- Interview Questions
- JavaScript
- What does an async function return, and how does await pause…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: An async function always returns a Promise: a returned value resolves it and a thrown error rejects it. await pauses only that async function until the awaited promise settles, and the main thread keeps running. The rest of the function then resumes later as a microtask.
Detailed explanation
Marking a function async changes two things. First, its return value is always wrapped in a Promise. If you write return 42, the caller receives a Promise that resolves to 42. If the function throws, the caller receives a rejected Promise instead of a synchronous exception. Second, you can use await inside it.
When the engine hits await somePromise, it does the following:
- If the value is not a promise, it is wrapped as if by
Promise.resolve(value). You can await plain values and thenables, not just promises. - The async function is suspended at that point. Its local variables and position are saved.
- Control returns to whoever called the function. That caller has already received the pending Promise.
- When the awaited promise settles, the rest of the function is queued as a microtask. It resumes once the current synchronous code finishes and the call stack is empty.
- If the awaited promise rejects,
awaitthrows that rejection reason at that line. This is whytry/catchworks with async/await.
The key point is that await does not block the JavaScript thread. Other code, event handlers, timers and rendering continue to run. Only the async function itself waits. This is very different from a blocking sleep in languages like Java or C.
Another detail interviewers like to probe: code before the first await runs synchronously, as soon as the function is called. An async function with no await at all runs fully synchronously. It still returns a Promise.
Example
console.log('1');
async function demo() {
console.log('2'); // runs synchronously
await null; // suspends demo, queues the rest as a microtask
console.log('4');
return 'done';
}
const result = demo();
console.log('3');
console.log(result instanceof Promise); // true
result.then(value => console.log(value)); // 'done'
// Output order: 1, 2, 3, true, 4, doneThe console.log('3') runs before '4'. Calling demo() returns immediately once it reaches await. The continuation only runs after the current synchronous script finishes.
Error handling follows the same model:
async function load() {
throw new Error('Network failed');
}
load().catch(err => console.log(err.message)); // 'Network failed'
async function main() {
try {
await load();
} catch (err) {
console.log('Caught:', err.message);
}
}Common mistakes
- Treating the result as the actual value. Writing
const user = getUser(); console.log(user.name)givesundefined, becauseuseris a Promise. You mustawaitit or use.then(). - Believing await freezes the whole program. It suspends only the current async function. Other code keeps running, which can cause race conditions if shared state changes in between.
- Using await at the top level of a regular script. Top-level
awaitworks only in ES modules, and in Node.js only in ES module files (.mjsor"type": "module"). In CommonJS or classic scripts it is a syntax error. - Expecting a try/catch around the call to catch async errors.
try { load(); } catch {}withoutawaitcatches nothing. The rejection happens later, on the returned Promise.
Follow-up questions
- What is the difference between
return promiseandreturn await promiseinside a try/catch block? - Why does a
.then()callback run before asetTimeout(fn, 0)callback? - Can you use
awaitinside a regular (non-async) callback such asarray.map?
Interview tip: Explain await in terms of the event loop: the function suspends, the caller gets a pending Promise, and the continuation runs as a microtask. Interviewers listen for exactly that sequence.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More easy JavaScript questions
Related Questions
- Why can't try/catch catch an error thrown inside a setTimeout callback or a promise? · JavaScript · 1 ans
- Why is using await inside a loop often slow, and how do you run async tasks in parallel? · JavaScript · 1 ans
- Why does a for loop with var and setTimeout print the same value every time? · JavaScript · 1 ans
- What is the temporal dead zone, and are let and const actually hoisted? · JavaScript · 1 ans
- What are tagged template literals in JavaScript and when would you actually use one? · JavaScript · 1 ans
- How do try, catch and finally work, and what happens if finally contains a return? · JavaScript · 1 ans
- What is the difference between localStorage, sessionStorage and cookies in the browser? · JavaScript · 1 ans
- How do HttpOnly, Secure and SameSite cookie flags protect against XSS and CSRF? · JavaScript · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)