You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we know, `fetch`returns a promise. And JavaScript generally has no concept of "aborting" a promise. So how can we cancel an ongoing `fetch`? E.g. if the user actions on our site indicate that the`fetch`isn't needed any more.
4
+
Як ми знаємо, `fetch`повертає проміс. А в JavaScript, як правило, немає концепції "переривання" промісу. Отже, як ми можемо перервати поточний `fetch`? Наприклад, якщо дії користувача на нашому сайті вказують на те, що`fetch`більше не потрібен.
5
5
6
-
There's a special built-in object for such purposes: `AbortController`. It can be used to abort not only `fetch`, but other asynchronous tasks as well.
6
+
Для таких цілей є спеціальний вбудований об’єкт: `AbortController`. Його можна використовувати для переривання не тільки `fetch`, але й інших асинхронних завдань.
7
7
8
-
The usage is very straightforward:
8
+
Його використання дуже просте:
9
9
10
-
## The AbortController object
10
+
## Об’єкт AbortController
11
11
12
-
Create a controller:
12
+
Створімо контролер:
13
13
14
14
```js
15
15
let controller =newAbortController();
16
16
```
17
17
18
-
A controller is an extremely simple object.
18
+
Контролер -- це надзвичайно простий об’єкт.
19
19
20
-
-It has a single method`abort()`,
21
-
-And a single property `signal` that allows to set event listeners on it.
20
+
-Він має єдиний метод`abort()`,
21
+
-І єдину властивість `signal`, що дозволяє встановлювати на ньому обробники подій.
let fetchJobs =urls.map(url=>fetch(url, { //fetches
133
+
let fetchJobs =urls.map(url=>fetch(url, { //запити fetch
134
134
signal:controller.signal
135
135
}));
136
136
137
-
//Wait for fetches and our task in parallel
137
+
//Чекаємо на виконання запитів fetch та наших завдань паралельно
138
138
let results =awaitPromise.all([...fetchJobs, ourJob]);
139
139
140
-
//if controller.abort() is called from anywhere,
141
-
//it aborts all fetches and ourJob
140
+
//якщо controller.abort() викликається з будь-якого місця,
141
+
//він перериває всі fetch та ourJob
142
142
```
143
143
144
-
## Summary
144
+
## Підсумки
145
145
146
-
-`AbortController`is a simple object that generates an`abort`event on it's`signal`property when the `abort()`method is called (and also sets `signal.aborted`to`true`).
147
-
-`fetch`integrates with it: we pass the`signal`property as the option, and then`fetch`listens to it, so it's possible to abort the`fetch`.
148
-
-We can use`AbortController`in our code. The "call`abort()`" -> "listen to`abort` event" interaction is simple and universal. We can use it even without`fetch`.
146
+
-`AbortController`-- це простий об’єкт, який генерує подію`abort`для своєї властивості`signal`під час виклику методу `abort()`(а також встановлює для `signal.aborted`значення`true`).
147
+
-`fetch`інтегрується з ним: ми передаємо властивість`signal`як параметр, а потім`fetch`прослуховує його, тому можна перервати цей`fetch`.
148
+
-Ми можемо використовувати`AbortController`у нашому коді. Взаємодія "виклик`abort()`" -> "прослуховування події`abort`" проста та універсальна. Ми можемо використовувати його навіть без`fetch`.
0 commit comments