/Async Programming — Callbacks, Promises & Async/Await
Concept Detail

Async Programming — Callbacks, Promises & Async/Await

Difficulty: hard

Overview


JavaScript is single-threaded with an event loop. Asynchronous work (timers, network, I/O) runs off the main thread and its callbacks are queued. A Promise represents a future value with states: pending, fulfilled, or rejected. .then() / .catch() / .finally() chain reactions. Promise.all() resolves when all promises resolve; Promise.race() resolves/rejects with the first settled promise. async functions always return a Promise; await pauses the function until the awaited Promise settles.

Practice Linked Questions


medium

Q1. What is a Promise in JavaScript?


Select one answer before revealing.

medium

Q2. What is the output? ```js Promise.resolve(42).then(val => console.log(val)); ```


Select one answer before revealing.

medium

Q3. What does the `async` keyword before a function declaration do?


Select one answer before revealing.

medium

Q4. What is the output? ```js async function main() { const result = await Promise.resolve(10); console.log(result); } main(); ```


Select one answer before revealing.

medium

Q5. How do you handle errors in async/await?


Select one answer before revealing.

hard

Q6. What does `Promise.all([p1, p2, p3])` do?


Select one answer before revealing.

hard

Q7. What is the output? ```js console.log("start"); setTimeout(() => console.log("timeout"), 0); Promise.resolve().then(() => console.log("promise")); console.log("end"); ```


Select one answer before revealing.

hard

Q8. What is the output? ```js const p = new Promise((resolve) => { resolve(1); resolve(2); }); p.then(val => console.log(val)); ```


Select one answer before revealing.