function* fetchData(){
while(true){
const {
payload} = yield take('fetchData')
yield call(func,...) // here, mySaga func cannot take actions until yield call finishes, so actions that are dispatched in-between will lost
// therefore, better to use yield fork instead call, like this
yield fock(func,...) //yield fork is a non-blocking method
}
}
An other solution, we can use takeEvery
to take actions simultaneously.
takeEvery
allows multiple actions(like fetchData
) instances to be started concurrently. At a given moment, we can start a new fetchData
task while there are still one or more previous fetchData
tasks which have not yet terminated.
If we want to only get the response of the latest request fired (e.g. to always display the latest version of data) we can use the takeLatest
.
takeLatest
allows only one fetchData task to run at any moment. And it will be the latest started task. If a previous task is still running when another fetchData
task is started, the previous task will be automatically cancelled.
Setting up your root Saga (global error handling)
V1.0 yield all implementation
export default function* rootSaga() {
yield all([
helloSaga(),
watchIncrementAsync()
])
// code after all-effect
}
Here, the all effect is used with an array and your sagas will be executed in parallel.