Facebook Pixel

async/await Interview Questions in JavaScript

async/await is a top interview topic. Here are the most asked questions with answers.

async/await Interview Questions in JavaScript

async/await is a top interview topic. Here are the most asked questions with answers.

Q1: What is async/await?

Syntactic sugar over promises. async functions return promises. await pauses until a promise settles. Makes async code look synchronous.

Q2: What is the output?

async function foo() { console.log("1"); await Promise.resolve(); console.log("2"); } foo(); console.log("3");

Answer: 1, 3, 2. await pauses foo; "3" logs first (sync), then "2" (microtask).

Q3: Does an async function always return a promise?

Yes. Return a value -> Promise.resolve(value). Throw -> Promise.reject(error). Return a promise -> returned as-is.

Q4: What is the output?

async function foo() { return 42; } foo().then((v) => console.log(v));

Answer: 42. async wraps the return value in a promise.

Q5: How do you handle errors with async/await?

Use try/catch around await. If any await rejects, the catch runs.

Q6: What is the output?

async function foo() { try { await Promise.reject("err"); } catch (err) { console.log("caught", err); } } foo();

Answer: caught err. try/catch catches the rejection.

Q7: How do you run async operations in parallel?

const [a, b] = await Promise.all([fetchA(), fetchB()]);

Q8: Does await block the main thread?

No. await pauses the async function and yields to the caller. The main thread continues.

Q9: What happens if you use await in forEach?

urls.forEach(async (url) => { await fetch(url); });

forEach does not await. The loop finishes immediately; fetches run in parallel in the background. Use for...of or Promise.all instead.

Q10: What is the difference between async/await and promises?

async/await is syntactic sugar over promises. Same mechanism, cleaner syntax. try/catch instead of .catch. Better stack traces. Works well with loops.

The Takeaway

async/await interview questions test: the definition, the 1, 3, 2 output (microtask), always returning a promise, error handling (try/catch), parallel (Promise.all), not blocking the main thread, forEach not awaiting, and the relationship to promises.

1, 3, 2. Code before await runs synchronously (1). await pauses the function and schedules the rest as a microtask. Code after the foo() call runs next (3, sync). Then the rest of foo runs (2, microtask).

Yes. If you return a value, it is wrapped in Promise.resolve. If you throw, it becomes Promise.reject. If you return a promise, it is returned as-is.

No. await pauses the async function and yields control to the caller. The caller continues running synchronous code. The rest of the async function runs as a microtask when the awaited promise settles.

forEach does not await the async callback. The loop finishes immediately, and the async callbacks run in parallel in the background. Use for...of for sequential await or Promise.all with map for parallel.

Use Promise.all: const [a, b] = await Promise.all([fetchA(), fetchB()]). Start all promises at once, then await all. This is much faster than sequential await for independent operations.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.