13-사가-조합

yield*는 사가 조합을 가능하게 해주지만, 한계가 있음

  • 뭉쳐진 제너레이터들을 분리하여 테스트 불가.

    • 테스트 코드 안의 중복, 실행의 오버헤드 발생.

  • yield*은 오직 순차적인 태스크들의 조합만 허용. 한 번에 1개의 제너레이터만 yield* 가능.

yield를 사용하여 1개 혹은 그 이상의 서브태스크들을 병렬 시작 가능.

function* fetchPosts() {
  yield put(actions.requestPosts())
  const products = yield call(fetchApi, '/products')
  yield put(actions.receivePosts(products))
}

function* watchFetch() {
  while (yield take(FETCH_POSTS)) {
    yield call(fetchPosts) // waits for the fetchPosts task to terminate
  }
}

배열 로 이뤄진 여러 제너레이터를 yield하면, 그 안의 서브 제너레이터들은 병렬로 시작.

  • 얘네들 다 끝나고, 반환된 모든 결과를 가지고 진행.

function* mainSaga(getState) {
  const results = yield all[call(task1), call(task2), ...]
  yield put(showResults(results))
}

Last updated

Was this helpful?