JavaScriptIntermediate#browser#async

How does fetch differ from XMLHttpRequest?

fetch is promise based, streams responses and works with async/await, but it only rejects on network failure — you must check response.ok yourself. XHR is callback based and more verbose.

Example
const res = await fetch('/api/x');
if (!res.ok) throw new Error(res.status);
const data = await res.json();

Related Questions

1
JavaScriptIntermediate#browser#security

What is CORS?

Open
2
JavaScriptBeginner#async

What is the difference between synchronous and asynchronous code?

Open
3
JavaScriptAdvanced#es6

What are generators and iterators?

Open