Module 11: JavaScript Interview Questions and Answers
Promises, Async/Await, and Event Loop Interview Questions
Answer common questions about asynchronous code, fetch, promises, and scheduling.
1. What is asynchronous JavaScript?#
Answer: It lets a program start work that completes later, such as network requests or timers, while the runtime continues other eligible work. The exact API and scheduling behaviour depend on the host environment.
2. What is a Promise?#
Answer: A promise represents an eventual result. It can be pending, fulfilled, or rejected. .then handles fulfillment, .catch handles rejection, and .finally runs after settlement regardless of outcome.
3. What does async do?#
Answer: An async function always returns a promise. Returning a value fulfills that promise with the value; throwing an error rejects it.
4. What does await do?#
Answer: await pauses execution inside an async function until the awaited value settles. It does not block the entire JavaScript runtime, although synchronous work before or after it still runs on the current thread.
5. Does fetch reject on HTTP 404?#
Answer: Usually no. fetch rejects for failures such as certain network errors, but HTTP error responses still fulfill with a Response. Check response.ok or response.status explicitly.
6. What is the difference between Promise.all and Promise.allSettled?#
Answer: Promise.all fulfills when all inputs fulfill and rejects when one input rejects. Promise.allSettled waits for every input and returns each fulfilled or rejected outcome.
7. What is the event loop?#
Answer: It is part of the runtime scheduling model that coordinates tasks, microtasks, and other work. A practical model is that current synchronous execution completes, promise reactions run as microtasks, and later tasks such as timers run when scheduled by the host.
8. What is a microtask?#
Answer: Promise reaction callbacks are commonly processed as microtasks. Microtasks are drained at defined scheduling points, often before the runtime takes another task. Exact host behaviour should be understood in the context of the environment.
9. Does setTimeout(..., 0) execute immediately?#
Answer: No. It schedules a callback to run after the current stack completes and the runtime can process the timer task. The actual delay can be longer than zero.
10. Is async/await parallel execution?#
Answer: No. It is syntax for working with promises. Independent asynchronous operations can be started together and awaited with Promise.all, but CPU-intensive JavaScript does not become parallel merely by marking a function async.
11. How should errors from an async function be handled?#
Answer: Await it in a try...catch block or attach a .catch handler to the returned promise. Handle the error where useful recovery or context is available; do not silently disguise failure as success.
12. What is AbortController used for?#
Answer: It can cancel supported asynchronous operations such as a fetch request. It is useful when a user changes a search, navigates away, or starts work that makes an earlier request unnecessary.