react-saga

리린·2021년 8월 12일
0

React

목록 보기
10/47

yield

  1. 제너레이터 함수.next()를 할 때마다 yield 앞까지 실행된다.
  2. 바로 앞 yield는 함수.next( ---여기에 있는 인자값---) 으로 대체된다.
  3. 재실행 시에도 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}

유용한 참고서

profile
개발자지망생

0개의 댓글