Home > Enterprise >  Is the next middleware called synchronously when the next function is called in Node Express
Is the next middleware called synchronously when the next function is called in Node Express

Time:01-31

I am trying to understand how the next function works in Node Express framework. I understand we use the next function in the current middleware to invoke the next middleware that we have mentioned. middlewares are nothing but callback functions passed to express.use/[METHOD]().


When we call next(), is the next middleware called synchronously? i.e does the next middleware run in the same process tick, or does the next middleware gets queued in the callback or microtask queue and is run at some later point of time.

CodePudding user response:

Yes the next middleware is called synchronously. They are running in the same process queue, it is working like a stack. You can find more informations here

CodePudding user response:

As Matthieu BRAULT mentioned, next calls the following middleware and its code runs synchronously. This can be proven by using the package: https://www.npmjs.com/package/tick-id. Here, we will get different numbers if we are on on different ticks.

  • Related