dcode
dcode

Reputation: 1

Dispatching Redux actions in order

I currently have an array of indexes and I would like to perform a dispatch action on each element. Other answers I've looked at seem to suggest using Promise.all. However, order matters and if I use Promise.all the promises are executed out of order. I tried an async and await and it works well, but I don't know if it's the best practice. Is this alright to do in an action?

const someAction = () => async (dispatch) => {
  for (const element of allElements) {
    await dispatch(doThisAction(element));
  }
}

Upvotes: 0

Views: 926

Answers (1)

markerikson
markerikson

Reputation: 67577

It's legal, but, we recommend that you should:

So, in this case, I would dispatch a single action containing all of the data from the response.

The exact mechanics involved depend on what you're trying to specifically accomplish here.

Upvotes: 1

Related Questions