Reputation: 3794
Have async increment saga, which increment counter after 3 seconds
function* incrementAsync() {
yield call(delay, 3000);
yield put(counterIncrement());
}
function* incrementAsyncWatcher() {
yield takeEvery(ASYNC_INCREMENT, incrementAsync);
}
function* incrementCounterSaga() {
yield fork(incrementAsyncWatcher);
}
const task = sagaMiddleware.run(incrementCounterSaga);
then I try to stop this saga
task.cancel();
store.dispathch({ type: ASYNC_INCREMENT });
but after 3 seconds see that saga is still working!
How to stop this saga?
Upvotes: 0
Views: 1889
Reputation: 1477
I found that explicit task cancellation can be really challenging since you have to pass and manage the task object.
Instead, I thought that patterns like this worked:
function* incrementAsyncWatcher() {
yield takeLatest(ASYNC_INCREMENT, incrementAsync);
}
takeLatest
will cancel the task, whereas takeEvery
creates a new task.
You can also lookup the docs for race
to get cancellation implicitly
Upvotes: 2