12.2.1 yield 표현식과 양방향 통신
호출자가 제너레이터에 정보를 전달할 수 있음
제너레이터는 그 정보에 따라 자신의 동작 방식 자체를 바꿀 수 있음
function* interrogate() {
const name = yield "What is your name?";
const color = yield "What is your favorite color?";
return `${name}'s favorite color is ${color}.`;
}
const it = interrogate();
it.next(); // {value: "What is your name?", done: false}
it.next('Ethan'); // {value: "What is your favorite color?", done: false}
it.next('orange'); // {value: "Ethan's favorite color is orange.", done: true}
Last updated