Skip to content

Commit 143ea24

Browse files
committed
[번역] Part1 11.6 11.7 11.8 충돌 해결 및 번역 수정
1 parent 71ab899 commit 143ea24

6 files changed

Lines changed: 34 additions & 136 deletions

File tree

1-js/11-async/06-promisify/article.md

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
기능을 구현 하다 보면 콜백보다는 프라미스가 더 편리하기 때문에 콜백 기반 함수와 라이브러리를 프라미스를 반환하는 함수로 바꾸는 게 좋은 경우가 종종 생길 겁니다.
66

7-
<<<<<<< HEAD
8-
<info:callbacks> 챕터에서 사용했던 `loadScript(src, callback)` 예시를 사용해 프라미스화에 대해 좀 더 자세히 알아봅시다.
9-
=======
10-
For better understanding, let's see an example.
7+
이해를 돕기 위해 예시를 하나 살펴봅시다.
118

12-
For instance, we have `loadScript(src, callback)` from the chapter <info:callbacks>.
13-
>>>>>>> upstream/master
9+
예를 들어 <info:callbacks> 챕터에서 사용했던 `loadScript(src, callback)`이 있다고 해봅시다.
1410

1511
```js run
1612
function loadScript(src, callback) {
@@ -27,19 +23,15 @@ function loadScript(src, callback) {
2723
// loadScript('path/script.js', (err, script) => {...})
2824
```
2925

30-
<<<<<<< HEAD
31-
`loadScript(src, callback)`를 이제 프라미스화해봅시다. 새로운 함수 `loadScriptPromise(src)``loadScript`와 동일하게 동작하지만 `callback`을 제외한 `src`만 인수로 받아야 하고, 프라미스를 반환해야 합니다.
32-
=======
33-
The function loads a script with the given `src`, and then calls `callback(err)` in case of an error, or `callback(null, script)` in case of successful loading. That's a widespread agreement for using callbacks, we saw it before.
34-
>>>>>>> upstream/master
26+
이 함수는 주어진 `src`의 스크립트를 불러온 뒤, 에러가 발생하면 `callback(err)`를 호출하고 로딩에 성공하면 `callback(null, script)`를 호출합니다. 앞서 살펴본 것처럼 콜백을 사용할 때 널리 쓰이는 방식입니다.
3527

36-
Let's promisify it.
28+
이제 이 함수를 프라미스화해봅시다.
3729

38-
We'll make a new function `loadScriptPromise(src)`, that does the same (loads the script), but returns a promise instead of using callbacks.
30+
새 함수 `loadScriptPromise(src)`를 만들겠습니다. 이 함수는 스크립트를 불러온다는 점에서 기존 함수와 동일하게 동작하지만, 콜백을 사용하는 대신 프라미스를 반환합니다.
3931

40-
In other words, we pass it only `src` (no `callback`) and get a promise in return, that resolves with `script` when the load is successful, and rejects with the error otherwise.
32+
다시 말해 `callback` 없이 `src`만 전달하면 프라미스를 반환받습니다. 로딩에 성공하면 이 프라미스는 `script`와 함께 이행되고, 실패하면 에러와 함께 거부됩니다.
4133

42-
Here it is:
34+
구현은 다음과 같습니다.
4335
```js
4436
let loadScriptPromise = function(src) {
4537
return new Promise((resolve, reject) => {
@@ -54,35 +46,19 @@ let loadScriptPromise = function(src) {
5446
// loadScriptPromise('path/script.js').then(...)
5547
```
5648

57-
<<<<<<< HEAD
58-
새롭게 구현한 `loadScriptPromise`는 프라미스 기반 코드와 잘 융화됩니다.
49+
예시에서 볼 수 있듯이 새 함수는 기존 함수 `loadScript`를 감싸는 래퍼입니다. 새 함수는 자체 콜백을 넘겨 `loadScript`를 호출하고, 이 콜백은 결과에 따라 프라미스의 `resolve``reject`를 호출합니다.
5950

60-
예시에서 볼 수 있듯이, `loadScriptPromise`기존 함수 `loadScript`에 모든 일을 위임합니다. `loadScript`의 콜백은 스크립트 로딩 상태에 따라 `이행` 혹은 `거부`상태의 프라미스를 반환합니다.
51+
이제 `loadScriptPromise`프라미스 기반 코드에 잘 어울립니다. 콜백보다 프라미스를 더 선호한다면, 그리고 곧 그럴 만한 이유를 더 살펴볼 예정인데, 이제 이 함수를 사용하면 됩니다.
6152

62-
그런데 실무에선 함수 하나가 아닌 여러 개의 함수를 프라미스화 해야 할 겁니다. 헬퍼 함수를 만드는 게 좋을 것 같네요. 프라미스화를 적용 할 함수 `f`를 받고 래퍼 함수를 반환하는 함수 `promisify(f)`를 만들어봅시다.
53+
실무에서는 함수 하나가 아니라 여러 함수를 프라미스화해야 할 수도 있으므로 헬퍼를 사용하는 편이 좋습니다.
6354

64-
`promisify(f)`가 반환하는 래퍼 함수는 위 예시와 동일하게 동작할 겁니다. 프라미스를 반환하고 호출을 기존 함수 `f`에 전달하여 커스텀 콜백 내의 결과를 추적해야 하죠.
55+
이 헬퍼를 `promisify(f)`라고 부르겠습니다. `promisify(f)`는 프라미스화할 함수 `f`를 받고 래퍼 함수를 반환합니다.
6556

6657
```js
6758
function promisify(f) {
68-
return function (...args) { // 래퍼 함수를 반환함
59+
return function (...args) { // 래퍼 함수를 반환함 (*)
6960
return new Promise((resolve, reject) => {
70-
function callback(err, result) { // f에 사용할 커스텀 콜백
71-
=======
72-
As we can see, the new function is a wrapper around the original `loadScript` function. It calls it providing its own callback that translates to promise `resolve/reject`.
73-
74-
Now `loadScriptPromise` fits well in promise-based code. If we like promises more than callbacks (and soon we'll see more reasons for that), then we will use it instead.
75-
76-
In practice we may need to promisify more than one function, so it makes sense to use a helper.
77-
78-
We'll call it `promisify(f)`: it accepts a to-promisify function `f` and returns a wrapper function.
79-
80-
```js
81-
function promisify(f) {
82-
return function (...args) { // return a wrapper-function (*)
83-
return new Promise((resolve, reject) => {
84-
function callback(err, result) { // our custom callback for f (**)
85-
>>>>>>> upstream/master
61+
function callback(err, result) { // f에 사용할 커스텀 콜백 (**)
8662
if (err) {
8763
reject(err);
8864
} else {
@@ -102,26 +78,18 @@ let loadScriptPromise = promisify(loadScript);
10278
loadScriptPromise(...).then(...);
10379
```
10480

105-
<<<<<<< HEAD
106-
위 예시는 프라미스화 할 함수가 인수가 두 개(`(err, result)`)인 콜백을 받을 것이라 가정하고 작성되었습니다. 십중팔구 이런 상황일 것이고, 커스텀 콜백은 이 상황에 딱 들어맞습니다. `promisify`가 잘 동작하는 것은 말할 것도 없겠죠.
107-
=======
108-
The code may look a bit complex, but it's essentially the same that we wrote above, while promisifying `loadScript` function.
81+
코드가 조금 복잡해 보일 수 있지만, 본질적으로는 위에서 `loadScript` 함수를 프라미스화할 때 작성한 코드와 같습니다.
10982

110-
A call to `promisify(f)` returns a wrapper around `f` `(*)`. That wrapper returns a promise and forwards the call to the original `f`, tracking the result in the custom callback `(**)`.
83+
`promisify(f)`를 호출하면 `f`를 감싸는 래퍼, 즉 `(*)`로 표시한 함수가 반환됩니다. 이 래퍼는 프라미스를 반환하고 원래 함수 `f`로 호출을 전달하며, 커스텀 콜백 `(**)`에서 결과를 추적합니다.
11184

112-
Here, `promisify` assumes that the original function expects a callback with exactly two arguments `(err, result)`. That's what we encounter most often. Then our custom callback is in exactly the right format, and `promisify` works great for such a case.
113-
>>>>>>> upstream/master
85+
여기서 `promisify`는 원래 함수가 정확히 두 개의 인수 `(err, result)`를 받는 콜백을 기대한다고 가정합니다. 이런 경우가 가장 흔합니다. 그러면 커스텀 콜백이 바로 알맞은 형식이 되고, 이런 경우 `promisify`는 잘 동작합니다.
11486

11587
그런데 함수 `f`가 두 개를 초과하는 인수를 가진 콜백, `callback(err, res1, res2, ...)`을 받는다면 어떤 일이 발생할까요?
11688

117-
<<<<<<< HEAD
118-
이런 경우를 대비하여 좀 더 진화한 헬퍼 함수, `promisify`를 만들어 봅시다. 새롭게 만든 함수를 `promisify(f, true)`형태로 호출하면, 프라미스 결과는 콜백의 성공 케이스(`results`)를 담은 배열, `[res1, res2, ...]`이 됩니다.
119-
=======
120-
We can improve our helper. Let's make a more advanced version of `promisify`.
89+
헬퍼를 개선해봅시다. 좀 더 발전된 버전의 `promisify`를 만들어 보겠습니다.
12190

122-
- When called as `promisify(f)` it should work similar to the version above.
123-
- When called as `promisify(f, true)`, it should return the promise that resolves with the array of callback results. That's exactly for callbacks with many arguments.
124-
>>>>>>> upstream/master
91+
- `promisify(f)`로 호출하면 위 버전과 비슷하게 동작해야 합니다.
92+
- `promisify(f, true)`로 호출하면 콜백 결과를 담은 배열로 이행되는 프라미스를 반환해야 합니다. 이는 인수가 많은 콜백을 위한 동작입니다.
12593

12694
```js
12795
// 콜백의 성공 결과를 담은 배열을 얻게 해주는 promisify(f, true)
@@ -149,22 +117,14 @@ f = promisify(f, true);
149117
f(...).then(arrayOfResults => ..., err => ...);
150118
```
151119
152-
<<<<<<< HEAD
153-
`callback(result)`같이 `err`이 없는 형태나 지금까지 언급하지 않은 형태의 이색적인 콜백도 있을 수 있는데, 이런 경우엔 헬퍼 함수를 사용하지 않고 직접 프라미스화 하면 됩니다.
154-
=======
155-
As you can see it's essentially the same as above, but `resolve` is called with only one or all arguments depending on whether `manyArgs` is truthy.
120+
위 코드와 본질적으로는 같습니다. 다만 `manyArgs`가 참 같은 값인지에 따라 `resolve`를 하나의 인수로 호출할지, 전체 인수로 호출할지만 달라집니다.
156121
157-
For more exotic callback formats, like those without `err` at all: `callback(result)`, we can promisify such functions manually without using the helper.
158-
>>>>>>> upstream/master
122+
`callback(result)`처럼 `err`가 아예 없는 콜백 등 더 특이한 콜백 형식의 경우, 헬퍼를 사용하지 않고 해당 함수를 직접 프라미스화하면 됩니다.
159123
160124
본 챕터에서 설명한 헬퍼 함수보다 더 유용한 형태의 프라미스화를 도와주는 함수를 제공하는 모듈도 많습니다. [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify)가 대표적인 예입니다. Node.js에선 내장 함수 `util.promisify`를 사용해 프라미스화를 할 수 있습니다.
161125
162126
```smart
163-
<<<<<<< HEAD
164-
프라미스화는 곧 배우게 될 `async/await`와 함께 사용하면 더 좋습니다. 다만, 콜백을 완전히 대체하지는 못한다는 사실을 기억해 두시기 바랍니다.
165-
=======
166-
Promisification is a great approach, especially when you use `async/await` (covered later in the chapter <info:async-await>), but not a total replacement for callbacks.
167-
>>>>>>> upstream/master
127+
프라미스화는 특히 이후 <info:async-await> 챕터에서 다룰 `async/await`와 함께 사용할 때 매우 유용한 접근법이지만, 콜백을 완전히 대체할 수는 없습니다.
168128

169129
프라미스는 하나의 결과만 가질 수 있지만, 콜백은 여러 번 호출할 수 있기 때문입니다.
170130

1-js/11-async/07-microtask-queue/article.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,14 @@ alert("코드 종료"); // 얼럿 창이 가장 먼저 뜹니다.
2323

2424
## 마이크로태스크 큐
2525

26-
<<<<<<< HEAD
2726
비동기 작업을 처리하려면 적절한 관리가 필요합니다. 이를 위해 ECMA에선 `PromiseJobs`라는 내부 큐(internal queue)를 명시합니다. V8 엔진에선 이를 '마이크로태스크 큐(microtask queue)'라고 부르기 때문에 이 용어가 좀 더 선호됩니다.
28-
=======
29-
Asynchronous tasks need proper management. For that, the ECMA standard specifies an internal queue `PromiseJobs`, more often referred to as the "microtask queue" (V8 term).
30-
>>>>>>> upstream/master
3127

3228
[명세서](https://tc39.github.io/ecma262/#sec-jobs-and-job-queues)의 설명을 살펴봅시다.
3329

3430
- 마이크로태스크 큐는 먼저 들어온 작업을 먼저 실행합니다(FIFO, first-in-first-out).
3531
- 실행할 것이 아무것도 남아있지 않을 때만 마이크로태스크 큐에 있는 작업이 실행되기 시작합니다.
3632

37-
<<<<<<< HEAD
3833
요약하자면, 어떤 프라미스가 준비되었을 때 이 프라미스의 `.then/catch/finally` 핸들러가 큐에 들어간다고 생각하시면 됩니다. 이때 핸들러들은 여전히 실행되지 않습니다. 현재 코드에서 자유로운 상태가 되었을 때에서야 자바스크립트 엔진은 큐에서 작업을 꺼내 실행합니다.
39-
=======
40-
Or, to put it more simply, when a promise is ready, its `.then/catch/finally` handlers are put into the queue; they are not executed yet. When the JavaScript engine becomes free from the current code, it takes a task from the queue and executes it.
41-
>>>>>>> upstream/master
4234

4335
위 예시에서 '코드 종료'가 먼저 출력되는 이유가 여기에 있습니다.
4436

@@ -48,11 +40,7 @@ Or, to put it more simply, when a promise is ready, its `.then/catch/finally` ha
4840

4941
여러 개의 `.then/catch/finally`를 사용해 만든 체인의 경우, 각 핸들러는 비동기적으로 실행됩니다. 큐에 들어간 핸들러 각각은 현재 코드가 완료되고, 큐에 적체된 이전 핸들러의 실행이 완료되었을 때 실행됩니다.
5042

51-
<<<<<<< HEAD
5243
**그렇다면 '프라미스 성공!'을 먼저, '코드 종료'를 나중에 출력되게 하려면 어떻게 해야 할까요?** 실행 순서가 중요한 경우엔 이런 요구사항이 충족되도록 코드를 작성해야 합니다.
53-
=======
54-
**What if the order matters for us? How can we make `code finished` appear after `promise done`?**
55-
>>>>>>> upstream/master
5644

5745
방법은 아주 쉽습니다. `.then`을 사용해 큐에 넣으면 됩니다.
5846

@@ -115,11 +103,7 @@ window.addEventListener('unhandledrejection', event => alert(event.reason));
115103

116104
## 요약
117105

118-
<<<<<<< HEAD
119-
모든 프라미스 동작은 '마이크로태스크 큐'(ES8 용어)라 불리는 내부 '프라미스 잡(promise job)' 큐에 들어가서 처리되기 때문에 프라미스 핸들링은 항상 비동기로 처리됩니다.
120-
=======
121-
Promise handling is always asynchronous, as all promise actions pass through the internal "promise jobs" queue, also called "microtask queue" (V8 term).
122-
>>>>>>> upstream/master
106+
모든 프라미스 동작은 '마이크로태스크 큐'(V8 용어)라 불리는 내부 '프라미스 잡(promise job)' 큐에 들어가서 처리되기 때문에 프라미스 핸들링은 항상 비동기로 처리됩니다.
123107

124108
따라서 `.then/catch/finally` 핸들러는 항상 현재 코드가 종료되고 난 후에 호출됩니다.
125109

1-js/11-async/08-async-await/02-rewrite-async-2/solution.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11

2-
<<<<<<< HEAD
3-
속임수랄게 없는 문제입니다. `demoGithubUser`안의 `.catch``try...catch`로 교체하고 필요한 곳에 `async/await`를 추가하면 됩니다.
4-
=======
5-
There are no tricks here. Just replace `.catch` with `try..catch` inside `demoGithubUser` and add `async/await` where needed:
6-
>>>>>>> upstream/master
2+
속임수랄게 없는 문제입니다. `demoGithubUser`안의 `.catch``try..catch`로 교체하고 필요한 곳에 `async/await`를 추가하면 됩니다.
73

84
```js run
95
class HttpError extends Error {
@@ -50,4 +46,4 @@ async function demoGithubUser() {
5046
}
5147

5248
demoGithubUser();
53-
```
49+
```

1-js/11-async/08-async-await/02-rewrite-async-2/task.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11

22
# async와 await를 사용해서 '다시 던지기' 예시 재작성하기
33

4-
<<<<<<< HEAD
5-
<info:promise-chaining> 챕터에서 다뤘던 '다시 던지기(rethrow)' 관련 예시를 기억하실 겁니다. 이 예시를 `.then/catch` 대신 `async/await`를 사용해 다시 작성해 봅시다.
6-
=======
7-
Below you can find the "rethrow" example. Rewrite it using `async/await` instead of `.then/catch`.
8-
>>>>>>> upstream/master
4+
아래는 '다시 던지기(rethrow)' 예시입니다. 이 예시를 `.then/catch` 대신 `async/await`를 사용해 다시 작성해 봅시다.
95

106
그리고 `demoGithubUser` 안의 반복(recursion)은 반복문(loop)을 사용해 작성하도록 합시다. `async/await`를 사용하면 쉽게 작성할 수 있습니다.
117

1-js/11-async/08-async-await/03-async-from-regular/task.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11

22
# async가 아닌 함수에서 async 함수 호출하기
33

4-
<<<<<<< HEAD
5-
'일반' 함수가 하나 있는데, 여기서 `async` 함수를 어떻게 하면 호출하고, 그 결과를 사용할 수 있을까요?
6-
=======
7-
We have a "regular" function called `f`. How can you call the `async` function `wait()` and use its result inside of `f`?
8-
>>>>>>> upstream/master
4+
'일반' 함수 `f`가 있습니다. `f` 안에서 `async` 함수 `wait()`를 호출하고 그 결과를 사용하려면 어떻게 해야 할까요?
95

106
```js
117
async function wait() {
@@ -15,15 +11,9 @@ async function wait() {
1511
}
1612

1713
function f() {
18-
<<<<<<< HEAD
1914
// ...코드...
2015
// async wait()를 호출하고 그 결과인 10을 얻을 때까지 기다리려면 어떻게 해야 할까요?
2116
// f는 일반 함수이기 때문에 여기선 'await'를 사용할 수 없다는 점에 주의하세요!
22-
=======
23-
// ...what should you write here?
24-
// we need to call async wait() and wait to get 10
25-
// remember, we can't use "await"
26-
>>>>>>> upstream/master
2717
}
2818
```
2919

0 commit comments

Comments
 (0)