yield
- 제너레이터 함수.next()를 할 때마다 yield 앞까지 실행된다.
- 바로 앞 yield는 함수.next( ---여기에 있는 인자값---) 으로 대체된다.
- 재실행 시에도 yield 앞까지 진행된다(즉, 구간별로 yield는 하나다)
function* watchGenerator(){
console.log('모니터링 중...');
let prevAction = null;
while(true){
const action = yield;
console.log('이전 액션: ', prevAction);
prevAction = action;
if(action.type==='HELLO'){
console.log('안녕하세요');
}
}
}
const watch=watchGenerator();
watch.next({type:'HELLO'});
//모니터링 중...
//{value: undefined, done: false}
watch.next({type:'HELLO'});
//이전 액션: null
// 안녕하세요
//{value: undefined, done: false}