Redux-saga: Q. rootSaga: yield watchSaga() VS fork(watchSaga) [VS takeEvery(action, saga)]?

Created on 22 Feb 2017  路  3Comments  路  Source: redux-saga/redux-saga

In the documentation (more precisely, in the beginner tutorial), the rootSaga is implemented as:

export default function* rootSaga() {
  yield [
    watchSagaOne(),
    watchSagaTwo(),
  ];
}

This Saga yields an array with the results of calling our two sagas[...]. This means the two resulting Generators will be started in parallel.

Great, I got it. Everything's work.

Now, let's take a look to the real-world example:

export default function* rootSaga() {
  yield [
    fork(watchSagaOne),
    fork(watchSagaTwo),
  ];
}

Hmm. Not the same.
The API documentation says that fork:

Creates an Effect description that instructs the middleware to perform a non-blocking call on fn

But, having the sagas run in parallel directly, I'm wasn't really expecting any blocking call.

I tried switching between having the watchSaga functions invoked directly or using fork (and even mixing the two), but I don't see any difference in the behaviour.

My question: what's the difference? Is there a _best way_ or a suggested use case scenario?
Is it related to tests, maybe?
Or maybe fork is the _redux-saga way_, and the other is the _js generator way_?

Also, (but this is more of a confirmation), using something like:

export default function* rootSaga() {
  yield [
    takeEvery(actionOne, sagaOne),
    takeEvery(actionTwo, sagaTwo),
  ];
}

looks like it's working great and we can get rid of the "watch" sagas (that is, if you don't need any additional "in-between" logic, of course).
Are there any (other) disadvantages in using takeEvery directly?

Thank you so much! :)

Most helpful comment

great question! I'd suggest searching the issues section. there's a similar discussion here

https://github.com/redux-saga/redux-saga/issues/760

docs also say that the "watch/fork" method allows for more powerful code patterns while takeEvery will guarantee that you fork a new task in response to a particular action

as for the diff btwn yielding generators directly vs the use of fork, the only difference I can see right now is that if fork returns a task descriptor, it lends you the ability to cancel it directly.

All 3 comments

great question! I'd suggest searching the issues section. there's a similar discussion here

https://github.com/redux-saga/redux-saga/issues/760

docs also say that the "watch/fork" method allows for more powerful code patterns while takeEvery will guarantee that you fork a new task in response to a particular action

as for the diff btwn yielding generators directly vs the use of fork, the only difference I can see right now is that if fork returns a task descriptor, it lends you the ability to cancel it directly.

Thank you @anthonychung14, I did search a little in the issues, but I completely missed #760: looks like I'm not the only one with doubts (I'm relieved)! :)
I'll surely dig into it, thanks! <3

Actually, I'm going to close this issue: the discussion in #760 is exactly what I was looking for. Thanks again!

Was this page helpful?
0 / 5 - 0 ratings