-
I am trying to come up with a clever scheme to nest sagas. I found that you can just E.g.: in the following example:
I wonder if this is a valid way of nesting sagas. Code excerpt: async function sleep1() {
await sleep(100);
}
function* saga2(x) {
console.log("saga2", x, 1);
yield call(sleep1);
console.log("saga2", x, 2);
yield call(sleep1);
console.log("saga2", x, 3);
}
const sagaCfgs = {
test1: {
nested: [
[saga2, "HI"],
[saga2, "HELLO"]
]
}
};
function* saga1(action) {
console.log(`start`);
const sagaCfgName = action.payload?.nested;
const sagaCfg = sagaCfgs[sagaCfgName]; // || "test1"
console.log(sagaCfgs, sagaCfg);
const nestedSagas = sagaCfg.nested;
try {
for (let i = 0; i < nestedSagas.length; ++i) {
yield call(...nestedSagas[i]);
}
} catch (error) {
yield put({ type: "FAILED", error });
}
console.log(`end`);
}
export default function* rootSaga() {
yield takeLatest("A", saga1);
} What does the Saga Documentation Say?From what I could find: the documentation is lacking. Most relevant are:
But they don't really explain whether calling a saga is a valid approach. I understand that I can just do |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It all depends on what kind of concurrency pattern are you trying to express. If you want to wait before executing a new saga in the sequence before starting a new one then yes - calling them one after another is the appropriate thing to do. If you keep them in some kind of a dynamic object~ then looping through them (with a call inside a loop) is probably the best what you can do. |
Beta Was this translation helpful? Give feedback.
It all depends on what kind of concurrency pattern are you trying to express. If you want to wait before executing a new saga in the sequence before starting a new one then yes - calling them one after another is the appropriate thing to do. If you keep them in some kind of a dynamic object~ then looping through them (with a call inside a loop) is probably the best what you can do.