Creates an Effect description that instructs the middleware to run a Race
between multiple Effects (this is similar to how
Promise.race([...])
behaves).
Example
The following example runs a race between two effects:
A call to a function fetchUsers which returns a Promise
A CANCEL_FETCH action which may be eventually dispatched on the Store
import { take, call, race } from redux-saga/effects
import fetchUsers from './path/to/fetchUsers'
If call(fetchUsers) resolves (or rejects) first, the result of race will
be an object with a single keyed object {response: result} where result
is the resolved result of fetchUsers.
If an action of type CANCEL_FETCH is dispatched on the Store before
fetchUsers completes, the result will be a single keyed object
{cancel: action}, where action is the dispatched action.
Notes
When resolving a race, the middleware automatically cancels all the losing
Effects.
Creates an Effect description that instructs the middleware to run a Race between multiple Effects (this is similar to how
Promise.race([...])
behaves).Example
The following example runs a race between two effects:
A call to a function
fetchUsers
which returns a PromiseA
CANCEL_FETCH
action which may be eventually dispatched on the Storeimport { take, call, race } from
redux-saga/effects
import fetchUsers from './path/to/fetchUsers'function* fetchUsersSaga() { const { response, cancel } = yield race({ response: call(fetchUsers), cancel: take(CANCEL_FETCH) }) }
If
call(fetchUsers)
resolves (or rejects) first, the result ofrace
will be an object with a single keyed object{response: result}
whereresult
is the resolved result offetchUsers
.If an action of type
CANCEL_FETCH
is dispatched on the Store beforefetchUsers
completes, the result will be a single keyed object{cancel: action}
, where action is the dispatched action.Notes
When resolving a
race
, the middleware automatically cancels all the losing Effects.