bhylu.com

Advertisements

JS Fetch API

Fetch requests a resource and gives you a Response. The example uses an offline Response so you can practise parsing without a network request.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

const response = new Response('{"title":"Repair club"}', {status: 200});
if (!response.ok) throw new Error("Request failed");
console.log((await response.json()).title);

What happens

The parsed title is Repair club. In a local project, replace the constructed response with await fetch of your endpoint.