The Fetch API - promise-based HTTP requests

One of the coolest async APIs introduced in browsers is the Fetch API, which is the modern replacement for the XMLHttpRequest API. Have you ever found yourself using jQuery just for simplifying AJAX requests with $.ajax? If you have, then this is surely a golden API for you, as it is natively easier to code and read!

If you remember, we created a promised version of XMLHttpRequest ourselves in Chapter 4, Asynchronous Programming. However, fetch comes natively, hence, there are performance benefits. Let's see how it works:

fetch(link) .then(data => {    // do something with data}).catch(err => {    // do something with error});

Awesome! So fetch uses promises! If that's the case, we can combine it with ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.