202
votes

I know that Fetch API uses Promises and both of them allow you to do AJAX requests to a server.

I have read that Fetch API has some extra features, which aren't available in XMLHttpRequest (and in the Fetch API polyfill, since it's based on XHR).

What extra capabilities does the Fetch API have?

3
Though I can't recall on the spot, there are one or two things you can do with XHR you can't with fetch. You say you have read that fetch has extra possibilities, those articles aren't very good if they don't say what they areJaromanda X
found the two things you can't do with fetch that you can with XHR ... you can't set your own value for request timeout in fetch, nor can you get progress eventsJaromanda X
Fetch is just a simplified way of doing things for most types of XMLHttpRequests. If your use case fits what Fetch does, then use it. When you get right down to it the XMLHttpRequest API is ugly for what most people use it for. Fetch was an effort to offer a cleaner way of doing things that doesn't need a library wrapped around XMLHttpRequest to make it palatable.jfriend00
It has pure support in browsers ( caniuse.com/#search=fetch ), so there is a polifill for it github.com/github/fetch, wich is working above xhrilyabasiuk
@Marco - How can you not say that fetch(url).then(function(data) (...)); is not simpler than using XMLHttpRequest to do the same thing? It may have lots of other features, but geez, it sure is simpler to use for common things. It IS a cleaned up API.jfriend00

3 Answers

140
votes

There are a few things that you can do with fetch and not with XHR:

  • You can use the Cache API with the request and response objects;
  • You can perform no-cors requests, getting a response from a server that doesn't implement CORS. You can't access the response body directly from JavaScript, but you can use it with other APIs (e.g. the Cache API);
  • Streaming responses (with XHR the entire response is buffered in memory, with fetch you will be able to access the low-level stream). This isn't available yet in all browsers, but will be soon.

There are a couple of things that you can do with XHR that you can't do yet with fetch, but they're going to be available sooner or later (read the "Future improvements" paragraph here: https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/):

  • Abort a request (this now works in Firefox and Edge, as @sideshowbarker explains in his comment);
  • Report progress.

This article https://jakearchibald.com/2015/thats-so-fetch/ contains a more detailed description.

74
votes

fetch

  • missing a builtin method to consume documents
  • no way to set a timeout yet
  • can't override the content-type response header
  • if the content-length response header is present but not exposed, the body's total length is unknown during the streaming
  • will call the signal's abort handler even if the request has been completed
  • no upload progress (support for ReadableStream instances as request bodies is yet to come)

XHR

  • there's no way to not send cookies (apart from using the non-standard mozAnon flag or the AnonXMLHttpRequest constructor)
  • can't return FormData instances
  • doesn't have an equivalent to fetch's no-cors mode
  • always follow redirects
13
votes

The answers above are good and provide good insights, but I share the same opinion as shared in this google developers blog entry in that the main difference (from a practical perspective) is the convenience of the built-in promise returned from fetch

Instead of having to write code like this

function reqListener() {
    var data = JSON.parse(this.responseText);
}

function reqError(err) { ... }

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.onerror = reqError;
oReq.open('get', './api/some.json', true);
oReq.send();

we can clean things up and write something a little more concise and readable with promises and modern syntax

fetch('./api/some.json')
    .then((response) => {
        response.json().then((data) => { 
            ... 
        });
    })
    .catch((err) => { ... });