Skip to content

Files

Latest commit

8e0d379 · Jan 20, 2025

History

History
24 lines (15 loc) · 671 Bytes

how_to_cancel_a_fetch_request.md

File metadata and controls

24 lines (15 loc) · 671 Bytes

How to cancel a fetch request?

You can cancel a fetch request using an AbortController. The AbortController allows you to abort one or more fetch requests.

Example:

let controller = new AbortController();
let signal = controller.signal;

fetch('https://api.example.com/data', { signal: signal })
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.log('Fetch aborted', err));

// To cancel the request
controller.abort();

Tags: advanced, JavaScript, Fetch API