Bhavya Bhatt
Bhavya Bhatt

Reputation: 57

Redux middleware handling multiple actions

I am using middleware in redux for some async logic execution. My question is what happens when a action is being processed by the middleware and another action is dispatched before the completion of the first one, what will happen in this case, whether the action will be put to halt till the middleware complete processing or whether that action will pass the middleware which will start executing for this action as well (along with the first one).

I am a beginner to react and redux and don't know if the answer should be obvious as it is not to me.

Upvotes: 0

Views: 199

Answers (1)

TLDR: It cannot happen because JS is a sync language. It operates using event loop and tasks queue. Each async task (i.e. receiving fetch response or event listener triggered by user) is placed in a queue and is executed one by one. So when some task is executed another will wait in queue.

Also keep in mind that async function is more then one task. Each await indicates a new task.

There are a lot of explanation of this model in the internet by keywords: "JS event loop" 1, 2

Upvotes: 1

Related Questions