DEV Community

Cover image for fetch API
Akash Pattnaik
Akash Pattnaik

Posted on

fetch API

This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.

fetch API

The Fetch API in browsers is for making HTTP requests. It's promise-based and cleaner than XMLHttpRequest. You can use fetch() with a URL and options to get data. Returns a promise with a Response object. Extract data with methods like .json().

Example Code (Not a part of explanation)

// Fetching data from a URL fetch('https://api.example.com/data') .then(response => { // Check if response is successful if (!response.ok) { throw new Error('Network response was not ok'); } // Extract JSON data from response return response.json(); }) .then(data => { // Work with the retrieved data console.log(data); }) .catch(error => { // Handle any errors that occur during the fetch operation console.error('Fetch error:', error); }); 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)