[JavaScript] 제너레이터 함수 ( function* )
function* : generator function 제너레이터 함수
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/function*
Generator 객체를 반환
yield
제너레이터 함수를 중지 또는 재개
yield*
다른 generator 또는 이터러블(iterable) 객체에 yield를 위임
다른 제너레이터 호출
//========================================
function* idMaker() {
var index = 0;
while (index < 3)
yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined
//======================================
//출력 제한
function* logGenerator() {
console.log(yield);
console.log(yield);
console.log(yield);
}
var gen = logGenerator();
// the first call of next executes from the start of the function
// until the first yield statement
gen.next();
gen.next('pretzel'); // pretzel
gen.next('california'); // california
gen.next('mayonnaise'); // mayonnaise
gen.next('mayonnaise2'); //출력 안됨