Hackforge Academy

Category: javascript

fetch() Method in JavaScript

Published on 15 Mar 2026

Explanation


The fetch() method is used to make HTTP
requests in JavaScript. It returns a Promise.

Code:

fetch('https://api.example.com/data');

Explanation


Handling JSON response using fetch().

Code:

fetch('https://api.example.com/data')
  .then(res => res.json())
  .then(data => console.log(data));

Explanation


Handling errors in fetch using catch().

Code:

fetch('https://api.example.com/data')
  .then(res => res.json())
  .catch(err => console.error(err));

Explanation


Using async/await with fetch for cleaner syntax.

Code:

async function getData() {
  const res = await 
fetch('https://api.example.com/data');
  const data = await res.json();
  console.log(data);
}

Explanation


Checking HTTP status before processing response.

Code:

fetch('https://api.example.com/data')
  .then(res => {
    if(!res.ok) throw new Error('Error');
    return res.json();
  })
  .then(data => console.log(data));

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community