

How to Abort a Fetch Request in JavaScript using AbortController
source link: https://typeofnan.dev/how-to-abort-a-fetch-request-in-javascript-using-abortcontroller/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sometimes it’s necessary to abort a fetch request. In this post, we explore how to quickly do so using AbortController!
A Simple Fetch Request
Let’s start out with a simple fetch request. We’ll grab some metadata about my Github account and log it to the console.
fetch('https://api.github.com/users/nas5w')
.then(res => res.json())
.then(data => {
console.log(data);
});
If we check our console, we see a json object describing my account has been logged. Here’s a bit of that data.
{"login":"nas5w","id":7538045,"node_id":"MDQ6VXNlcjc1MzgwNDU=","avatar_url":"https://avatars2.githubusercontent.com/u/7538045?v=4","gravatar_id":"","url":"https://api.github.com/users/nas5w","html_url":"https://github.com/nas5w"...}
Using AbortController
In this same scenario, we can create a new instance of the AbortController
object and pass fetch
a reference to the AbortController
instance’s signal
object.
Here’s what I mean:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.github.com/users/nas5w', { signal })
.then(res => res.json())
.then(data => {
console.log(data);
});
So now, fetch
has a reference to the signal
object on our controller instance. We can abort our fetch by calling abort
on our controller:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.github.com/users/nas5w', { signal })
.then(res => res.json())
.then(data => {
console.log(data);
});
controller.abort();
If we run this, we no longer log the returned data because we have immediately aborted our fetch
request!
Handling the Cancellation
You may have noticed in our last code snippet that our fetch
request isn’t gracefully aborted, we actually see an error in our console:
Uncaught (in promise) DOMException: The user aborted a request.
In order to handle this cancellation error, we simply need to catch
our error:
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.github.com/users/nas5w', { signal })
.then(res => res.json())
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
controller.abort();
Now if we run our code we see that we gracefully log our error:
DOMException: The user aborted a request.
So now we have successfully aborted our fetch request and caught the associated error.
A Note on Browser Compatibility
Most modern browsers have full support for AbortController
but (of course) if you have to support IE you’re out of luck! Be sure to check out the associated MDN docs for full compatibility info.
Recommend
-
10
We might want to use AbortController to abort multiple fetch requests in JavaScript. How might we accomplish this? A Quick Refresh: Aborting One Request Let’s quickly refresh ourselves on how to abort one fetch re...
-
7
AbortController polyfill for cancelling fetch() Jul 24, 2017 While writing tests for a React app I ran into the following warning: Warning: setState(...): Can only update a mounted or mounting c...
-
14
Contributor sirupsen commented
-
4
Copy link Member flip1995 commented...
-
10
JavaScript
-
5
How to abort API requests in JavaScriptMay 28th, 2022 | 3 mins read#javascript
-
6
TIL — Removing DOM Event Handlers using AbortController TIL — Removing DOM Event Handlers using AbortController Published: 2022.06.18 | 1 minutes read Thanks to
-
1
abort immediately on bad mem::zeroed/uninit #105997
-
3
by zhangxinxu from https://www.zhangxinxu.com/wordpress/?p=10694 鑫空间-鑫生活 本文欢迎分享与聚合,全文转载...
-
8
All ArticlesAbortController API: A Modern Approach to JavaScript Lifecycle EventsNew JavaSc...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK