Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take multiple actions without knowing which one is dispatched first?

Tags:

redux-saga

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.

like image 548
romseguy Avatar asked May 12 '17 15:05

romseguy


People also ask

Which function is used to avoid multiple component update due to multiple actions dispatched to the store?

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.

Which Redux-saga function is used to create dispatch side effects?

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.


1 Answers

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.

like image 143
Alex Avatar answered Sep 28 '22 01:09

Alex