SlideShare a Scribd company logo
1 of 25
Download to read offline
ECMAScript 2015
by jbee
[ES6] 10. Generator
Generator는무엇인가?
 Generator function 으로반환된값을 Generator Object 라고 하고
이 Generator Obejct 는 iterator 프로토콜을따르고 있다. 즉
 [Symbol.iterator] 가 프로퍼티에추가되어있다는것이다.  Generator
function 안에서는 yield 라는키워드를사용해서함수내부에작성된코
드를전부실행하지않는다. 제너레이터함수는 yield 를기준으로실행을나
누어서진행한다.  iterator 프로토콜을따르고 있기 때문에순차적으로실
행할수있는것이다.
cf> Generator function 를  제너레이터 함수 로,  Generator
Object 를  제너레이터 오브젝트 로 표기.
Generator Function
 function* 로표현할수있으며, 작성할때는일반 function 처럼선언문
과 표현식으로작성할수있다.
function* calc(prev, post) {
console.log("generator start");
yield prev + post;
}
or
let calc = function*(prev, post) {
console.log("generator start");
yield prev + post;
}
제너리에터함수에의해반환되는값은제너레이터오브젝트이다.
let generator = calc(1, 2);
console.log(typeof generator); // object
제너레이터오브젝트를반환하는순간에는오브젝트를반환하기만할뿐, 내부
코드는실행되지않는다.
Generator Object
 new 키워드를사용하여인스턴스를생성할수없다.
let cal = new calc();// Error
//TypeError: calc is not a constructor
next
 next() 메소드를통해제너레이터함수를실행시킬수있다.
console.log(generator.next());
//generator start
//{ value: 3, done: false }
console.log(generator.next());
//{ value: undefined, done: true }
value, done
 iterator 의 next() 메소드를실행시킨것처럼 value 와 done 이라는
프로퍼티를갖고 있는객체로반환된다. 그런데첫번째 next() 메소드실행
시에는 generator start 가 출력되었는데, 두번째실행시에는출력되지
않았다.  yield 라는키워드를중심으로함수가 나눠실행되는것이다.
yield 키워드, 함수를실행하고 멈출수있다.
[returnValue] = yield[expression]
위와같은구문으로 yield 를작성할수있다.
위의예제코드에서살펴봤듯이,  next() 메소드의반환값은 value 와
 done 으로구성되어있는오브젝트이다. 제너레이터의메소드 next() 에서
이두가지의값은yield에의해결정된다.  value 가 결정되는규칙이조금 복
잡하다.
value 결정규칙
 expression 으로반환되는값이할당.
이때,  expression 에있는값이 returnValue 에할당되지않는다.
 expression 에아무것도없으면 undefined 가 할당.
이때,  next() 의파라미터로넘겨지는값이 returnValue 에할당된
다.
done 결정규칙
계속수행할 yield 가 남아있으면 false .
더이상실행할 yield 가 없으면 true .
예제 코드를 살펴보자.
function* calc(prev, post) {
let result = 0;
console.log(`Initial result: ${result}`);
result = yield prev + post;
console.log(`Middle result: ${result}`);
result = yield;
console.log(`Last result: ${result}`);
}
let generator = calc(10, 20);
위에서언급한규칙에대한내용을모두담고 있는예제코드이다.
 console.log() 에는어떠한값이찍히게 될까? 코드를통해하나씩살펴보
자.
console.log(generator.next());
// Initial result: 0
// { value: 30, done: false }
 next() 메소드를실행시키면첫번째yield까지실행한다.
초기  result 변수에대한값이출력되고,
 expression 으로계산된값인 30 이 value 이출력된다.
아직 yield 가 남았으니 done 은 false 가 되겠다.
console.log(generator.next());
// Middle result: undefined
// { value: undefined, done: false }
두번째yield까지실행한다.
 expression 값이 result 에할당되지않은것을확인할수있다.
아직 yield 가 남았으니 done 은 false 가 되겠다.
console.log(generator.next(20));
// Last result: 20
// { value: undefined, done: true }
남은yield가 없으므로brace까지실행한다.
 next() 메소드의파라미터로넘겨진 20 이 result 변수에할당된것을
확인할수있다.
더이상 yield 키워드가 없으므로 done 은 true 가 된다.
yield 대신return
function* calc(prev, post) {
return prev + post;
}
let generator = calc(10, 20);
console.log(generator.next());
// { value: 30, done: true }
 return 키워드뒤에오는값이 value 에할당되고  yield 키워드의유무
와상관없이 done 에는 true 가 할당된다.  return 은수행되고 있는이터
레이터를종료시키는역할을수행한다.
이터레이터종료하기
 yield 키워드의유무와상관없이이터레이터를종료하고자할때는제너레
이터오브젝트의 throw() 메소드와 return() 메소드를사용할수있다.
function* idMaker(prev, post) {
let value = 0;
while(true) {
yield ++value;
}
}
let g = idMaker();
console.log(g.next());// { value: 1, done: false }
console.log(g.next());// { value: 2, done: false }
console.log(g.next());// { value: 3, done: false }
console.log(g.return(100));// { value: 100, done: true }
 return() 메소드의파라미터로넘어가는값이 value 에할당된다.
위의예제코드에 return() 대신 throw() 를호출하게 되면파라미터로
넘겨준Error Message를출력하고 이터레이터가 바로종료된다.
function* idMaker(prev, post) {
let value = 0;
try {
while(true) {
yield ++value;
}
} catch(e) {
console.log(`Error message: ${e}`);
}
}
let g = idMaker();
console.log(g.next());// { value: 1, done: false }
console.log(g.next());// { value: 2, done: false }
console.log(g.next());// { value: 3, done: false }
console.log(g.throw("Throw Exception"));
//Error message: Throw Exception
//{ value: undefined, done: true }
여기서 try-catch 구문에 yield 를추가하면어떻게 될까?
바로이터레이터가 종료되지않고  yield 다음의구문이실행된다.
function* idMaker(prev, post) {
let value = 0;
try {
while(true) {
yield ++value;
}
} catch(e) {
yield e;
}
}
let g = idMaker();
console.log(g.next());// { value: 1, done: false }
console.log(g.next());// { value: 2, done: false }
console.log(g.next());// { value: 3, done: false }
console.log(g.throw("Throw Exception"));
//{ value: 'Throw Exception', done: false }
console.log(g.next());// { value: undefined, done: true }
yield* 키워드
 yield 에 * 를붙인다음 [expression] 에이터러블오브젝트를작성할
수있다. 이렇게 되면해당 yield 가 수행될때이터러블오브젝트를순회하
게 된다. 코드를통해살펴보자.
우선적으로배열을순회한후에, 다음에해당하는 yield 를수행하게 된다.
function* gen() {
yield 1;
yield* [10, 20, 30];
yield 2;
}
let g = gen();
console.log(g.next());// { value: 1, done: false }
console.log(g.next());// { value: 10, done: false }
console.log(g.next());// { value: 20, done: false }
console.log(g.next());// { value: 30, done: false }
console.log(g.next());// { value: 2, done: false }
console.log(g.next());// { value: undefined, done: true }
마무리
문법을아는것과 실제프로그래밍에서적용하는것은확실히다른문제이다.
지금  yield 의향연을보고 이걸 어디에다가 쓰나하는생각이들것이다. 다
음링크들을참고하면좀나아질것 같아서, 몇가지링크를첨부한다.
ES6의제너레이터를사용한비동기 프로그래밍
Javascript의Generator와Koa.js
자바스크립트와비동기 오류처리
Reference
MDN function*
10. end

More Related Content

What's hot

What's hot (19)

Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
 
7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍
 
track2 04. MS는 Rx를 왜 만들었을까? feat. RxJS/ 네이버, 김훈민
track2 04. MS는 Rx를 왜 만들었을까? feat. RxJS/ 네이버, 김훈민 track2 04. MS는 Rx를 왜 만들었을까? feat. RxJS/ 네이버, 김훈민
track2 04. MS는 Rx를 왜 만들었을까? feat. RxJS/ 네이버, 김훈민
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
 
[Swift] Functions
[Swift] Functions[Swift] Functions
[Swift] Functions
 
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
 
React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
 
7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성7가지 동시성 모델 - 데이터 병렬성
7가지 동시성 모델 - 데이터 병렬성
 
Angular js 의존관계 주입과 서비스
Angular js 의존관계 주입과 서비스Angular js 의존관계 주입과 서비스
Angular js 의존관계 주입과 서비스
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리
 
골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료
 
Angular2 가기전 Type script소개
 Angular2 가기전 Type script소개 Angular2 가기전 Type script소개
Angular2 가기전 Type script소개
 
Refactoring - Chapter 8.2
Refactoring - Chapter 8.2Refactoring - Chapter 8.2
Refactoring - Chapter 8.2
 
ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는
 
[C++]2 variables andselectionstatement
[C++]2 variables andselectionstatement[C++]2 variables andselectionstatement
[C++]2 variables andselectionstatement
 
스위프트 성능 이해하기
스위프트 성능 이해하기스위프트 성능 이해하기
스위프트 성능 이해하기
 
[ES6] 12. Array
[ES6] 12. Array[ES6] 12. Array
[ES6] 12. Array
 

Similar to [ES6] 10. Generator

[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
탑크리에듀(구로디지털단지역3번출구 2분거리)
 

Similar to [ES6] 10. Generator (20)

Redux
ReduxRedux
Redux
 
헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리헷갈리는 자바스크립트 정리
헷갈리는 자바스크립트 정리
 
Airflow를 이용한 데이터 Workflow 관리
Airflow를 이용한  데이터 Workflow 관리Airflow를 이용한  데이터 Workflow 관리
Airflow를 이용한 데이터 Workflow 관리
 
Tdd 4장
Tdd 4장Tdd 4장
Tdd 4장
 
06장 함수
06장 함수06장 함수
06장 함수
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
Xe hack
Xe hackXe hack
Xe hack
 
Javascript 교육자료 pdf
Javascript 교육자료 pdfJavascript 교육자료 pdf
Javascript 교육자료 pdf
 
프론트엔드스터디 E05 js closure oop
프론트엔드스터디 E05 js closure oop프론트엔드스터디 E05 js closure oop
프론트엔드스터디 E05 js closure oop
 
Android Google Cloud Message 설정
Android Google Cloud Message 설정Android Google Cloud Message 설정
Android Google Cloud Message 설정
 
[2011 04 11]mock_object 소개
[2011 04 11]mock_object 소개[2011 04 11]mock_object 소개
[2011 04 11]mock_object 소개
 
Side Effect in Compose (Android Jetpack)
Side Effect in Compose (Android Jetpack)Side Effect in Compose (Android Jetpack)
Side Effect in Compose (Android Jetpack)
 
Boost라이브러리의내부구조 20151111 서진택
Boost라이브러리의내부구조 20151111 서진택Boost라이브러리의내부구조 20151111 서진택
Boost라이브러리의내부구조 20151111 서진택
 
React 실무활용 이야기
React 실무활용 이야기React 실무활용 이야기
React 실무활용 이야기
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수
 
[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초
 
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원
 
[JS] Function.prototype.bind
[JS] Function.prototype.bind[JS] Function.prototype.bind
[JS] Function.prototype.bind
 
JUnit & AssertJ
JUnit & AssertJJUnit & AssertJ
JUnit & AssertJ
 

More from Han JaeYeab

More from Han JaeYeab (19)

07. type system
07. type system07. type system
07. type system
 
06. decorator
06. decorator06. decorator
06. decorator
 
05. generics in typescript
05. generics in typescript05. generics in typescript
05. generics in typescript
 
04. interface in typescript
04. interface in typescript04. interface in typescript
04. interface in typescript
 
03. function in typescript
03. function in typescript03. function in typescript
03. function in typescript
 
02. class in typescript
02. class in typescript02. class in typescript
02. class in typescript
 
01. basic types
01. basic types01. basic types
01. basic types
 
intro. typescript playground
intro. typescript playgroundintro. typescript playground
intro. typescript playground
 
[ES6] 11. Modularization, import와 export
[ES6] 11. Modularization, import와 export[ES6] 11. Modularization, import와 export
[ES6] 11. Modularization, import와 export
 
[ES6] 9. Iterator
[ES6] 9. Iterator[ES6] 9. Iterator
[ES6] 9. Iterator
 
[ES6] 8. Symbol
[ES6] 8. Symbol[ES6] 8. Symbol
[ES6] 8. Symbol
 
[ES6] 7. Template literal
[ES6] 7. Template literal[ES6] 7. Template literal
[ES6] 7. Template literal
 
[ES6] 6. Class
[ES6] 6. Class[ES6] 6. Class
[ES6] 6. Class
 
[ES6] 5. Destructuring
[ES6] 5. Destructuring[ES6] 5. Destructuring
[ES6] 5. Destructuring
 
[ES6] 4. Spread, Rest parameter
[ES6] 4. Spread, Rest parameter[ES6] 4. Spread, Rest parameter
[ES6] 4. Spread, Rest parameter
 
[ES6] 3. iteration
[ES6] 3. iteration[ES6] 3. iteration
[ES6] 3. iteration
 
[ES6] 2. arrow function
[ES6] 2. arrow function[ES6] 2. arrow function
[ES6] 2. arrow function
 
[ES6] 1. let과 const
[ES6] 1. let과 const[ES6] 1. let과 const
[ES6] 1. let과 const
 
클라우드 컴퓨팅에 대한 기본적인 이해
클라우드 컴퓨팅에 대한 기본적인 이해클라우드 컴퓨팅에 대한 기본적인 이해
클라우드 컴퓨팅에 대한 기본적인 이해
 

Recently uploaded

Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
Wonjun Hwang
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
Wonjun Hwang
 

Recently uploaded (6)

A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)A future that integrates LLMs and LAMs (Symposium)
A future that integrates LLMs and LAMs (Symposium)
 
캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차캐드앤그래픽스 2024년 5월호 목차
캐드앤그래픽스 2024년 5월호 목차
 
Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)Merge (Kitworks Team Study 이성수 발표자료 240426)
Merge (Kitworks Team Study 이성수 발표자료 240426)
 
Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)Console API (Kitworks Team Study 백혜인 발표자료)
Console API (Kitworks Team Study 백혜인 발표자료)
 
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
Continual Active Learning for Efficient Adaptation of Machine LearningModels ...
 
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution DetectionMOODv2 : Masked Image Modeling for Out-of-Distribution Detection
MOODv2 : Masked Image Modeling for Out-of-Distribution Detection
 

[ES6] 10. Generator