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

1

5

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.

Zone answered 12/5, 2017 at 15:38 Comment(0)
R
10

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.

Roti answered 13/5, 2017 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.