Using the Fetch API
We have already seen the Fetch API being used in Chapter 4, Service Workers – Notification, Synchronization, and Our Podcast App, so let's do a quick review. Fetch is a modern replacement for XMLHttpRequest
. It is asynchronous, relying on promises, and provides a more streamline interface to manage dynamic requests. You can customize requests by creating custom Request
and header
objects. Service workers rely on Fetch to make network requests.
The fetch()
method takes two parameters, the URL that you are requesting (or a request
object) and an options
object. This method returns a promise
object.
There are also some differences from how you may be accustomed to making AJAX requests. Fetch does not usually throw an exception on HTTPS status codes, only if there is a network problem making the request, which typically indicates a platform or hardware issue. Any response from the server, even if it is an error status code such as 404: Page Not Found or 500: Server Error is...