I am trying to write some code that will execute several blocking sagas in sequence. For my use case, saga1 must complete before saga2 can start executing. Here is some code that shows a simplified version of what I am trying to do:
function* logger() {
console.log('spy 1');
}
function* logger2() {
console.log('spy2');
}
function* spy1() {
yield takeEvery('*', logger);
}
function* spy2() {
yield takeEvery('*', logger2);
}
export default function* rootSaga() {
yield call(spy1);
yield call(spy2);
}
When I dispatch an action, I only ever reach the first console.log. I know that if I use fork() instead of call() I can get both to run, however I do not want them to run in parallel. How can I make my first logger function complete and allow saga to move on to the second.
Thanks!
What about creating a single watcher that simulates what takeEvery does but in a sequencial manner?
function* logger() {
yield delay(1000);
console.log("logger 1");
}
function* logger2() {
console.log("logger 2");
}
function* watcher() {
while (yield take("*")) {
yield call(logger);
yield call(logger2);
}
}
function* rootSaga() {
yield call(watcher);
}
One of the points I'd like to bring here is that, first is that takeEvery uses a fork internally and then according to takeEvery's documentation:
There is no guarantee that the tasks will terminate in the same order they were started
so maybe using takeEvery won't accomplish what you need.