Skip to content

Commit 076ed60

Browse files
committed
docs: Fetch: Abort 번역
Signed-off-by: chayeoi <[email protected]>
1 parent 11e275e commit 076ed60

File tree

1 file changed

+57
-55
lines changed

1 file changed

+57
-55
lines changed

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

+57-55
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,133 @@
1+
# Fetch: 중단
12

2-
# Fetch: Abort
3+
알다시피 `fetch`는 프라미스를 반환합니다. 그리고 자바스크립트에는 일반적으로 프라미스 "중단"이라는 개념이 없습니다. 그렇다면 사이트에서 `fetch`가 더는 필요하지 않음을 나타내는 사용자 액션이 일어날 경우, 진행 중인 `fetch`를 어떻게 중단할 수 있을까요?
34

4-
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.
5+
이러한 경우를 위한 `AbortContoller`라고 하는 특별한 내장 객체가 있습니다. 이 객체는 `fetch`를 비롯한 다른 비동기 작업을 중단하는 데 사용할 수 있습니다.
56

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.
7+
사용법은 매우 직관적입니다.
78

8-
The usage is very straightforward:
9+
## AbortController 객체
910

10-
## The AbortController object
11-
12-
Create a controller:
11+
컨트롤러를 생성합니다.
1312

1413
```js
1514
let controller = new AbortController();
1615
```
1716

18-
A controller is an extremely simple object.
17+
컨트롤러는 매우 단순한 객체입니다.
18+
19+
- 단일 메서드인 `abort()`를 갖습니다.
20+
- `abort`에 대한 이벤트 리스너를 설정할 수 있는 단일 속성 `signal`을 갖습니다.
1921

20-
- It has a single method `abort()`,
21-
- And a single property `signal` that allows to set event liseners on it.
22+
`abort()`가 호출되면
2223

23-
When `abort()` is called:
24-
- `controller.signal` emits the `"abort"` event.
25-
- `controller.signal.aborted` property becomes `true`.
24+
- `controller.signal``"abort"` 이벤트를 내보냅니다.
25+
- `controller.signal.aborted` 속성은 `true`가 됩니다.
2626

27-
Generally, we have two parties in the process:
28-
1. The one that performs an cancelable operation, it sets a listener on `controller.signal`.
29-
2. The one one that cancels: it calls `controller.abort()` when needed.
27+
일반적으로 이 프로세스는 두 부분으로 나뉩니다.
3028

31-
Here's the full example (without `fetch` yet):
29+
1. `controller.signal`에 설정한 리스너에서 취소 가능한 연산을 수행하는 부분
30+
2. 필요할 때 `controller.abort()`를 호출하여 취소하는 부분
31+
32+
다음은 전체 예시입니다. (아직 `fetch`는 사용하지 않음)
3233

3334
```js run
3435
let controller = new AbortController();
3536
let signal = controller.signal;
3637

37-
// The party that performs a cancelable operation
38-
// gets "signal" object
39-
// and sets the listener to trigger when controller.abort() is called
40-
signal.addEventListener('abort', () => alert("abort!"));
38+
// "signal" 객체를 받아서
39+
// controller.abort()가 호출되었을 때 실행할 리스너를 설정하는
40+
// 취소 가능한 연산을 수행하는 부분
41+
signal.addEventListener("abort", () => alert("abort!"));
4142

42-
// The other party, that cancels (at any point later):
43+
// 취소하는 부분 (나중에 어떤 시점에라도)
4344
controller.abort(); // abort!
4445

45-
// The event triggers and signal.aborted becomes true
46+
// 이벤트가 트리거되고 signal.aborted는 true가 됩니다.
4647
alert(signal.aborted); // true
4748
```
4849

49-
As we can see, `AbortController` is just a means to pass `abort` events when `abort()` is called on it.
50+
보다시피 `AbortController``abort()`가 호출될 때 `abort` 이벤트를 전달하는 수단일 뿐입니다.
5051

51-
We could implement same kind of event listening in our code on our own, without `AbortController` object at all.
52+
`AbortController` 객체를 사용하지 않더라도 동일한 형태로 이벤트 수신을 구현할 수 있습니다.
5253

53-
But what's valuable is that `fetch` knows how to work with `AbortController` object, it's integrated with it.
54+
그러나 `AbortController`가 가치있는 이유는 `fetch``AbortController` 객체와 함께 작동하는 방법을 알고 있고, 통합되어 있기 때문입니다.
5455

55-
## Using with fetch
56+
## fetch와 함께 사용하기
5657

57-
To become able to cancel `fetch`, pass the `signal` property of an `AbortController` as a `fetch` option:
58+
`fetch`를 취소하려면 `AbortController``signal`속성을 `fetch` 옵션으로 전달합니다.
5859

5960
```js
6061
let controller = new AbortController();
6162
fetch(url, {
62-
signal: controller.signal
63+
signal: controller.signal,
6364
});
6465
```
6566

66-
The `fetch` method knows how to work with `AbortController`. It will listen to `abort` events on `signal`.
67+
`fetch` 메서드는 `AbortController`와 함께 작동하는 방법을 알고 있습니다. `fetch``signal`에 대한 `abort` 이벤트를 수신합니다.
6768

68-
Now, to to abort, call `controller.abort()`:
69+
중단하려면 `controller.abort()`를 호출합니다.
6970

7071
```js
7172
controller.abort();
7273
```
7374

74-
We're done: `fetch` gets the event from `signal` and aborts the request.
75+
끝났습니다. `fetch``signal`로부터 이벤트를 받고 요청을 중단합니다.
7576

76-
When a fetch is aborted, its promise rejects with an error `AbortError`, so we should handle it, e.g. in `try..catch`.
77+
`fetch`가 중단되면 프라미스는 `AbortError`와 함께 reject되므로, `try..catch` 같은 방식으로 에러를 적절히 처리해야합니다.
7778

78-
Here's the full example with `fetch` aborted after 1 second:
79+
다음은 1초 후에 `fetch`를 중단하는 전체 예시입니다.
7980

8081
```js run async
8182
// abort in 1 second
8283
let controller = new AbortController();
8384
setTimeout(() => controller.abort(), 1000);
8485

8586
try {
86-
let response = await fetch('/article/fetch-abort/demo/hang', {
87-
signal: controller.signal
87+
let response = await fetch("/article/fetch-abort/demo/hang", {
88+
signal: controller.signal,
8889
});
89-
} catch(err) {
90-
if (err.name == 'AbortError') { // handle abort()
90+
} catch (err) {
91+
if (err.name == "AbortError") {
92+
// abort() 처리
9193
alert("Aborted!");
9294
} else {
9395
throw err;
9496
}
9597
}
9698
```
9799

98-
## AbortController is scalable
100+
## AbortController는 확장 가능합니다
99101

100-
`AbortController` is scalable, it allows to cancel multiple fetches at once.
102+
`AbortController`는 확장 가능하며 한 번에 fetch 여러 개를 동시에 취소할 수 있습니다.
101103

102-
Here's a sketch of code that fetches many `urls` in parallel, and uses a single controller to abort them all:
104+
다음은 많은 `urls`을 병렬로 fetch하고 단일 컨트롤러를 사용하여 모두 중단하는 코드입니다.
103105

104106
```js
105-
let urls = [...]; // a list of urls to fetch in parallel
107+
let urls = [...]; // 병렬로 fetch할 url 목록
106108

107109
let controller = new AbortController();
108110

109-
// an array of fetch promises
111+
// fetch 프라미스 배열
110112
let fetchJobs = urls.map(url => fetch(url, {
111113
signal: controller.signal
112114
}));
113115

114116
let results = await Promise.all(fetchJobs);
115117

116-
// if controller.abort() is called from elsewhere,
117-
// it aborts all fetches
118+
// 어딘가 다른 곳에서 controller.abort()가 호출되면
119+
// 모든 fetch를 중단합니다
118120
```
119121

120-
If we have our own asynchronous tasks, different from `fetch`, we can use a single `AbortController` to stop those, together with fetches.
122+
만약 `fetch`가 아닌 자체 비동기 태스크가 있을 경우에도 단일 `AbortController`를 사용하여 fetch와 함께 중지할 수 있습니다.
121123

122-
We just need to listen to its `abort` event in our tasks:
124+
그저 작업 내에서 `abort` 이벤트를 수신하도록 하면 됩니다.
123125

124126
```js
125127
let urls = [...];
126128
let controller = new AbortController();
127129

128-
let ourJob = new Promise((resolve, reject) => { // our task
130+
let ourJob = new Promise((resolve, reject) => { // 자체 태스크
129131
...
130132
controller.signal.addEventListener('abort', reject);
131133
});
@@ -134,15 +136,15 @@ let fetchJobs = urls.map(url => fetch(url, { // fetches
134136
signal: controller.signal
135137
}));
136138

137-
// Wait for fetches and our task in parallel
139+
// fetch와 자체 태스크를 병렬로 기다립니다
138140
let results = await Promise.all([...fetchJobs, ourJob]);
139141

140-
// if controller.abort() is called from elsewhere,
141-
// it aborts all fetches and ourJob
142+
// 어딘가 다른 곳에서 controller.abort()가 호출되면
143+
// 모든 fetch와 자체 태스크를 중단합니다
142144
```
143145

144-
## Summary
146+
## 요약
145147

146-
- `AbortController` is a simple object that generates `abort` event on it's `signal` property when `abort()` method is called (and also sets `signal.aborted` to `true`).
147-
- `fetch` integrates with it: we pass `signal` property as the option, and then `fetch` listens to it, so it becomes 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`.
148+
- `AbortController``abort ()` 메서드가 호출될 때 자신의 `signal` 속성에 `abort` 이벤트를 발생시키는 단순한 객체입니다(또한 `signal.aborted``true`로 설정).
149+
- `fetch`와 통합: `signal` 속성을 옵션으로 전달하면 `fetch``abort` 이벤트를 수신하므로 `fetch`를 중단할 수 있습니다.
150+
- 코드에서 `AbortController`를 사용할 수 있습니다. "`abort()` 호출" -> "`abort` 이벤트 수신" 인터랙션은 단순하고 보편적입니다. 심지어 `fetch` 없이도 사용할 수 있습니다.

0 commit comments

Comments
 (0)