How to cancel a fetch request in JavaScript

1 Answer

0 votes
const controller = new AbortController();
const { signal } = controller;

fetch("http://localhost:8080", { signal }).then(response => {
    console.log(`Request complete`);
}).catch(e => {
    console.warn(`Fetch error: ${e.message}`);
});

controller.abort();




/*
run:

"Fetch error: The operation was aborted. "

*/

 



answered Dec 13, 2021 by avibootz
...