How do I wait for both ACTION_A and ACTION_B to be dispatched without knowing which one was dispatched first?
I tried const result = yield take([ACTION_A, ACTION_B])
but result
is only the first dispatched action, whereas I need both actions.
I tried const {a, b} = yield race({a: yield take(ACTION_A), b: yield take(ACTION_B)})
but if a
is defined, b
is not.
Remember I can't simply yield take(ACTION_A); yield take(ACTION_B)
because I don't know which one comes first.
Redux notifies subscribers after each successfully dispatched action, and amongst those subscribers will be every connect ed component in the app. To optimise this, we can utilise redux-batched-subscribe , which is a store enhancer that lets you debounce subscriber calls for multiple dispatches.
The Saga's generator function is named 'mySaga'. It uses a Redux-Saga effect called 'take', which is blocking the execution of the Saga until someone dispatches the action given as a parameter.
You can use the all
helper. all
creates an Effect description that instructs the middleware to run multiple Effects in parallel and wait for all of them to complete.
Your code could look like this:
function* mySaga() {
const [actionA, actionB] = yield all([
take(ACTION_A),
take(ACTION_B),
])
}
I hope my answer was helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With