A Task
object returned by a previous fork
Creates an Effect description that instructs the middleware to cancel previously forked tasks.
It wraps the array of tasks in cancel effects, roughly becoming the
equivalent of yield tasks.map(t => cancel(t))
.
A Task
is the object returned by a previous fork
Creates an Effect description that instructs the middleware to cancel a task
in which it has been yielded (self-cancellation). It allows to reuse
destructor-like logic inside a finally
blocks for both outer
(cancel(task)
) and self (cancel()
) cancellations.
function* deleteRecord({ payload }) { try { const { confirm, deny } = yield call(prompt); if (confirm) { yield put(actions.deleteRecord.confirmed()) } if (deny) { yield cancel() } } catch(e) { // handle failure } finally { if (yield cancelled()) { // shared cancellation logic yield put(actions.deleteRecord.cancel(payload)) } } }
Creates an Effect description that instructs the middleware to cancel a previously forked task.
Notes
To cancel a running task, the middleware will invoke
return
on the underlying Generator object. This will cancel the current Effect in the task and jump to the finally block (if defined).Inside the finally block, you can execute any cleanup logic or dispatch some action to keep the store in a consistent state (e.g. reset the state of a spinner to false when an ajax request is cancelled). You can check inside the finally block if a Saga was cancelled by issuing a
yield cancelled()
.Cancellation propagates downward to child sagas. When cancelling a task, the middleware will also cancel the current Effect (where the task is currently blocked). If the current Effect is a call to another Saga, it will be also cancelled. When cancelling a Saga, all attached forks (sagas forked using
yield fork()
) will be cancelled. This means that cancellation effectively affects the whole execution tree that belongs to the cancelled task.cancel
is a non-blocking Effect. i.e. the Saga executing it will resume immediately after performing the cancellation.For functions which return Promise results, you can plug your own cancellation logic by attaching a
[CANCEL]
to the promise.The following example shows how to attach cancellation logic to a Promise result:
import { CANCEL } from 'redux-saga' import { fork, cancel } from 'redux-saga/effects'
function myApi() { const promise = myXhr(...)
}
function* mySaga() {
}
redux-saga will automatically cancel jqXHR objects using their
abort
method.