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
`function *` is the syntax used to create a *generator function*. Calling a generator function returns a *generator object*. The generator object just follows the [iterator][iterator]interface (i.e. the`next`, `return` and `throw` functions).
This is the part of generators that is truly exciting. It essentially allows a function to pause its execution and pass control (fate) of the remainder of the function execution to the caller.
44
42
45
-
A generator function does not execute when you call it. It just creates a generator object. Consider the following example along with a sample execution:
43
+
일부 제네레이터 함수는 정말 흥미진진합니다. 기본적으로 함수가 실행을 일시중지하고 나머지 함수실행을 호출자에게 전달할 수 있습니다.
44
+
45
+
호출해도 제네레이터 함수가 실행되지 않습니다. 그것은 단지 제네레이터 객체를 생성합니다. 샘플 실행과 함께 다음 예제를 고려하십시요.
46
46
47
47
```ts
48
-
function* generator(){
49
-
console.log('Execution started');
50
-
yield0;
51
-
console.log('Execution resumed');
52
-
yield1;
53
-
console.log('Execution resumed');
48
+
function* generator(){
49
+
console.log('Execution started')
50
+
yield0
51
+
console.log('Execution resumed')
52
+
yield1
53
+
console.log('Execution resumed')
54
54
}
55
55
56
-
var iterator =generator();
57
-
console.log('Starting iteration');// This will execute before anything in the generator function body executes
* The function only starts execution once `next` is called on the generator object.
77
-
* The function *pauses* as soon as a `yield` statement is encountered.
78
-
* The function *resumes* when `next` is called.
76
+
- 제네레이터 함수를 시작하는 방법은 오직 `next`를 이용해서 호출할때 실행되며 이것은 제네레이터 객체에 해당됩니다.
77
+
- 제네레이터 함수를 중지하는 방법은 `yield`를 이용해야 합니다.
78
+
- 제레레이터 함수를 다시 시작하려면 `next`를 호출해야 합니다.
79
79
80
-
> So essentially the execution of the generator function is controllable by the generator object.
80
+
> 본질적으로 제네레이터 함수의 실행은 제네레이터 객체에 의해 제어 가능합니다.
81
81
82
-
Our communication using the generator has been mostly one way with the generator returning values for the iterator. One extremely powerful feature of generators in JavaScript is that they allow two way communications (with caveats).
82
+
제네레이터를 사용한 우리가 배웠던 방법은 대부분 이터레이터를 이용한 값을 반환하는 한가지 방법입니다. 자바스크립트의 제네레이터에는 하나의 매우 강력한 기능은 양방향 통신을 허용한다는 것입니다.
83
83
84
-
* you can control the resulting value of the `yield`expression using `iterator.next(valueToInject)`
85
-
* you can throw an exception at the point of the `yield`expression using `iterator.throw(error)`
84
+
- 당신은 `iterator.next(valueToInject)`를 이용해서 `yield`표현식의 결과값을 제어 할 수 있습니다.
85
+
- 당신은 `iterator.throw(error)`를 이용해서 `yield`표현식의 시점에서 예외처리를 할 수 있습니다.
86
86
87
-
The following example demonstrates `iterator.next(valueToInject)`:
87
+
`iterator.next(valueToInject)`를 예제에서 증명하고 있습니다.
88
88
89
89
```ts
90
90
function* generator() {
91
-
const bar =yield'foo';// bar may be *any* type
92
-
console.log(bar);// bar!
91
+
const bar =yield'foo'// bar may be *any* type
92
+
console.log(bar) // bar!
93
93
}
94
94
95
-
const iterator =generator();
95
+
const iterator =generator()
96
96
// Start execution till we get first yield value
97
-
const foo =iterator.next();
98
-
console.log(foo.value);// foo
97
+
const foo =iterator.next()
98
+
console.log(foo.value) // foo
99
99
// Resume execution injecting bar
100
-
const nextThing =iterator.next('bar');
100
+
const nextThing =iterator.next('bar')
101
101
```
102
102
103
-
Since `yield` returns the parameter passed to the iterator's `next` function, and all iterators' `next` functions accept a parameter of any type, TypeScript will always assign the `any` type to the result of the `yield` operator (`bar` above).
103
+
`yield`는 이터레이터의 `next`함수에 전달된 매개변수를 반환하기 때문에 그리고 모든 이터레이터는 `next`함수는 모든 유형의 매개변수를 허용하고, 타입스크립트는 `yield`연산자의 결과에 `any`타입을 할당합니다.
104
104
105
-
> You are on your own to coerce the result to the type you expect, and ensure that only values of that type are passed to next (such as by scaffolding an additional type-enforcement layer that calls `next` for you.) If strong typing is important to you, you may want to avoid two-way communication altogether, as well as packages that rely heavily on it (e.g., redux-saga).
105
+
> 당신은 당신이 기대하는 타입으로 결과를 강요하려고 합니다. 그리고 그것은 오직 값만 전달하는지 확인하십시요. 만약 당신이 강력한 타이핑이 중요한 경우에는 양방향 바인딩을 피하고 패키지를 많이 사용하는 패키지를 피할 수 있습니다. (예: redux-saga)
106
106
107
-
The following example demonstrates `iterator.throw(error)`:
107
+
`iterator.throw(error)`의 예제를 아래에서 증명합니다.
108
108
109
109
```ts
110
110
function* generator() {
111
111
try {
112
-
yield'foo';
113
-
}
114
-
catch(err) {
115
-
console.log(err.message); // bar!
112
+
yield'foo'
113
+
} catch (err) {
114
+
console.log(err.message) // bar!
116
115
}
117
116
}
118
117
119
-
var iterator =generator();
118
+
var iterator =generator()
120
119
// Start execution till we get first yield value
121
-
var foo =iterator.next();
122
-
console.log(foo.value);// foo
120
+
var foo =iterator.next()
121
+
console.log(foo.value) // foo
123
122
// Resume execution throwing an exception 'bar'
124
-
var nextThing =iterator.throw(newError('bar'));
123
+
var nextThing =iterator.throw(newError('bar'))
125
124
```
126
125
127
-
So here is the summary:
128
-
*`yield` allows a generator function to pause its communication and pass control to an external system
129
-
* the external system can push a value into the generator function body
130
-
* the external system can throw an exception into the generator function body
126
+
여기에 설명이 있습니다.
127
+
128
+
- 제네레이터 함수는 `yield`를 통해 통신을 일시중지하고 외부 시스템에 제어를 전달할 수 있게 합니다.
129
+
- 외부 시스템에서 값을 제네레이터 함수안으로 밀어넣을수 있습니다.
130
+
- 외부 시스템에서 예외를 제네레이터 함수안으로 던질 수 있습니다.
131
131
132
-
How is this useful? Jump to the next section [**async/await**][async-await] and find out.
132
+
이것은 어떻게 활용할 수 있나요? 다음장에서 [**async/await**][async-await]으로 이동하여 알아보십시요.
0 commit comments