Skip to content

Commit b2bbd1c

Browse files
committed
minor
1 parent 5b9f6b0 commit b2bbd1c

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

5-network/04-fetch-abort/article.md

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
# Fetch: Abort
33

4-
Aborting a `fetch` is a little bit tricky. Remember, `fetch` returns a promise. And JavaScript generally has no concept of "aborting" a promise. So how can we cancel a fetch?
4+
As we know, `fetch` returns a promise. And JavaScript generally has no concept of "aborting" a promise. So how can we abort a `fetch`?
55

6-
There's a special built-in object for such purposes: `AbortController`.
6+
There's a special built-in object for such purposes: `AbortController`, that can be used to abort not only `fetch`, but other asynchronous tasks as well.
77

88
The usage is pretty simple:
99

@@ -13,9 +13,16 @@ The usage is pretty simple:
1313
let controller = new AbortController();
1414
```
1515

16-
A controller is an extremely simple object. It has a single method `abort()`, and a single property `signal`. When `abort()` is called, the `abort` event triggers on `controller.signal`:
16+
A controller is an extremely simple object.
1717

18-
Like this:
18+
- It has a single method `abort()`, and a single property `signal`.
19+
- When `abort()` is called:
20+
- `abort` event triggers on `controller.signal`
21+
- `controller.signal.aborted` property becomes `true`.
22+
23+
All parties interested to learn about `abort()` call set listeners on `controller.signal` to track it.
24+
25+
Like this (without `fetch` yet):
1926

2027
```js run
2128
let controller = new AbortController();
@@ -26,7 +33,7 @@ The usage is pretty simple:
2633
2734
controller.abort(); // abort!
2835
29-
alert(signal.aborted); // true (after abort)
36+
alert(signal.aborted); // true
3037
```
3138

3239
- Step 2: pass the `signal` property to `fetch` option:
@@ -38,7 +45,7 @@ The usage is pretty simple:
3845
});
3946
```
4047

41-
Now `fetch` listens to the signal.
48+
The `fetch` method knows how to work with `AbortController`, it listens to `abort` on `signal`.
4249

4350
- Step 3: to abort, call `controller.abort()`:
4451

@@ -48,7 +55,7 @@ The usage is pretty simple:
4855

4956
We're done: `fetch` gets the event from `signal` and aborts the request.
5057
51-
When a fetch is aborted, its promise rejects with an error named `AbortError`, so we should handle it:
58+
When a fetch is aborted, its promise rejects with an error `AbortError`, so we should handle it, e.g. in `try..catch`:
5259
5360
```js run async
5461
// abort in 1 second
@@ -83,28 +90,32 @@ let fetchJobs = urls.map(url => fetch(url, {
8390
8491
let results = await Promise.all(fetchJobs);
8592
86-
// from elsewhere:
87-
// controller.abort() stops all fetches
93+
// if controller.abort() is called from elsewhere,
94+
// it aborts all fetches
8895
```
8996
90-
If we have our own jobs, different from `fetch`, we can use a single `AbortController` to stop those, together with fetches.
97+
If we have our own asynchronous jobs, different from `fetch`, we can use a single `AbortController` to stop those, together with fetches.
9198
99+
We just need to listen to its `abort` event:
92100
93101
```js
94102
let urls = [...];
95103
let controller = new AbortController();
96104
97-
let ourJob = new Promise((resolve, reject) => {
105+
let ourJob = new Promise((resolve, reject) => { // our task
98106
...
99107
controller.signal.addEventListener('abort', reject);
100108
});
101109
102-
let fetchJobs = urls.map(url => fetch(url, {
110+
let fetchJobs = urls.map(url => fetch(url, { // fetches
103111
signal: controller.signal
104112
}));
105113
114+
// Wait for fetches and our task in parallel
106115
let results = await Promise.all([...fetchJobs, ourJob]);
107116
108-
// from elsewhere:
109-
// controller.abort() stops all fetches and ourJob
117+
// if controller.abort() is called from elsewhere,
118+
// it aborts all fetches and ourJob
110119
```
120+
121+
So `AbortController` is not only for `fetch`, it's a universal object to abort asynchronous tasks, and `fetch` has built-in integration with it.

0 commit comments

Comments
 (0)