Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Fetching Data – `fetch` API & JSO | JavaScript Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. JavaScript Course

Lesson 15 of 55

·javascript

Fetching Data – `fetch` API & JSON

11 mins

Course progress27%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `fetch` **does not** reject the promise on HTTP errors – you must check `response.ok`.
  • Use **`AbortController`** to cancel a fetch request (useful for debouncing or component cleanup).
  • For older browsers, polyfill **`whatwg-fetch`** or use a library like **axios**.

The modern fetch API replaces XMLHttpRequest.

fetch('https://api.example.com/users')
  .then(res => {
    if (!res.ok) throw new Error('Network error');
    return res.json();
  })
  .then(users => console.table(users));
  • fetch returns a Promise that resolves with a Response object.
  • Use response.json() to parse JSON; response.text() for raw text.
  • Request options: method, headers, body.

Example: POST JSON data

fetch('/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ username, password })
})
.then(r => r.json())
.then(data => console.log(data));
Make it stickPractice what you've learned1 quiz · 7 practical tests · Optional activities from JavaScript CourseBrowse activities
Previous lessonAsynchronous JavaScript – Callbacks, Promises & async/awaitContinue to lesson 16Error Handling – try/catch, Custom Errors & DebuggingBack to JavaScript Course curriculum