1.

What Is Order Of Middleware Calls?

Answer»

One of the most important things about middleware in KOA is that the order in which they are written/included in your file, are the order in which they are executed DOWNSTREAM. As SOON as we hit a yield statement in a middleware, it switches to the next middleware in line till we reach the last. Then again we start moving BACK up and resuming functions from yield statements.

For example, in the following code snippet, the first FUNCTION executes first till yield, then the second middleware till yield, then the third. As we have no more middlewares here, we start moving back up, executing in reverse order, ie, third, second, first.

One of the most important things about middleware in koa is that the order in which they are written/included in your file, are the order in which they are executed downstream. As soon as we hit a yield statement in a middleware, it switches to the next middleware in line till we reach the last. Then again we start moving back up and resuming functions from yield statements.

For example, in the following code snippet, the first function executes first till yield, then the second middleware till yield, then the third. As we have no more middlewares here, we start moving back up, executing in reverse order, ie, third, second, first.



Discussion

No Comment Found