DEV Community

Lance Jian
Lance Jian

Posted on

Combine Javascript Generators with yield

It's not appropriate to call yield anotherSaga() inside a saga function to combine two sagas. In general, to combine two generators, we need to use yield* anotherSaga() . Or use redux-saga's call or fork instead.

function* g1() {
  yield 2;
  yield 3;
  yield 4;
}
function* g2() {
  yield 1;
  yield g1(); // if not use yield* g1()
  yield 5;
}
const gen = g2();
console.log(gen.next()); // > {value: 1, done: false}
console.log(gen.next()); // > {value: g1, done: false} not as you expected 
console.log(gen.next()); // > {value: 5, done: false}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)