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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay