Home > front end >  Is it good to use await before dispatch hooks in redux?
Is it good to use await before dispatch hooks in redux?

Time:10-14

I have a situation where i want to run 2 dispatch events one after another. There is no dependancy between 2.

Can i run something like below?

await dispatch(firstEvent)
dispatch(secondEvent)

I want to run secondEvent only after the first one.

Is there any better approach to call this other than using await? I have tried using callback from firstevent and all. ( Please suggest best practiced )

CodePudding user response:

Try this

const callApis = async () => {
   dispatch(
    apiOneAction(param1, param2), //get call first
   ).then(() => {
     dispatch(apiTwoAction(param3, param4)) // get call when first api returns response
  })   
}

CodePudding user response:

Dispatch is synchronous, so the await keyword gets you nothing here. You could use the redux-thunk library to get the async flow you're looking for.

  • Related