Question presented to candidate: "The term 'AJAX' predates fetch and Promises entirely. What was the original API used to make an AJAX request, and why would you choose fetch over it in new code today?"
What a strong answer should cover:
- 📌 Interview term: AJAX (Asynchronous JavaScript And XML) — not a specific API itself, but the general TECHNIQUE of fetching/sending data to a server in the background, without a full page reload — despite the name, modern AJAX overwhelmingly uses JSON, not XML.
- 📌 Interview term:
XMLHttpRequest(XHR) — the original, classic browser API for making an AJAX request, predating bothfetchand Promises — it uses an event-based, callback-driven model (onreadystatechange/onload/onerror), tracking progress through a numericreadyState. - 📌 Interview term:
fetch()— the modern, Promise-based replacement, covered in more depth in this bank's own dedicated fetch API question — genuinely built into every current browser and modern Node.js, verified directly. - 📌 Interview term: the real, direct answer to why fetch is generally preferred — a cleaner, Promise-based interface (genuinely composable with
async/await, chaining, andPromise.all) versus XHR's older, more verbose, callback/event-based model — verified directly thatfetchgenuinely returns a real Promise instance. - A precise answer names a real, genuine advantage XHR still has over plain
fetch: XHR provides a native upload/download progress event (onprogress), something plainfetchdoes not expose as simply — this is a real, honest reason some file-upload code still reaches for XHR (or a library built on it) today, rather than presentingfetchas strictly superior in every respect.
Clarifying questions expected:
- None — this is a definitional/historical-technical question; naming the real API that predates
fetch(XHR) and the concrete reason for preferringfetchtoday is the strong signal.
Code / implementation expected: Optional — confirming fetch is genuinely available and Promise-returning, contrasted with XMLHttpRequest's absence in this Node environment, demonstrates real understanding of the historical/environment context.