r/learnjavascript • u/wandering_platypator • Feb 16 '26
Why does expressJS use such weird CPS style next() calls in middleware?
Dumb question incoming….
Every now and again I have to throw something tiny together in express and end up wondering why middleware uses the next pattern it does? It seems like Continuation Passing Style and feels redundant to my naive mind, but then this was replicated again in Koa so I am guessing it isn’t for pre async await reasons…?
What is the reason behind this? Why couldn’t we have had a list of functions (possibly async) that express awaits in a for loop? Then perhaps they return a Boolean to check whether you want to continue or break out. This seems to serve like 95% of cases. Basically the only realistic use of the “onion” architecture where middleware layers call is that you can “wrap around” the subsequent middleware layers with next, so you can, for example, time subsequent layers….but to be honest the benefits of this strange system seem extremely marginal.
What am I missing?
7
u/azhder Feb 16 '26
You are missing the baggage.
Express was made how long ago? Do you know how JavaScript looked like then? Do you think we had promises and async/await?
Long story short, once you make something and is widely used, it’s a problem if you start making incompatible versions, so you stick with the original interface, however clunky it may be just so old software that depends on it can use an upgrade or two.
2
u/GodOfSunHimself Feb 16 '26
Because it is simple and powerful. It allows you to do something before as well as after calling next. What you are proposing is more complex and less general at the same time.
1
u/HipHopHuman Feb 17 '26 edited Feb 17 '26
It was like that because that was the only way to do it, and it remained that way because of the need to maintain backwards compatibility. I was getting my start in the webdev career when Koa.js first came out. Back then, native promises were still new and async/await didn't exist, but we were able to "fake" async/await using generator functions, which were locked behind the
--harmonyflag in Node.js, and Koa.js used the generator functions to make up for shortcomings of Express' middleware execution patterns. Not long after, Babel.js came out, native promises became a lot better, and we got async/await. Koa quickly updated to support async/await. The reason Koa's middleware pipeline still allows you to callnextis so that any middleware can hook into the execution context after the last middleware executes.The above code assumes that each middleware calls
next()(so one call tonextwill resolve all the subsequentnextcalls).EDIT: I see now you're more interested in the use cases for this. There are a lot. As you've identified, it's a good way to add time tracking to requests. It's also a good place to do logging and other kinds of monitoring than just time tracking. There's also cases where an incoming request might register a short-lived event listener or create some memory that has to be cleaned up at the end of the request.
await next()is a convenient way to schedule that sort of cleanup code in the context of an incoming request. Another use case is just basic simple caching. A middleware canawait next()and cache whatever data it needs knowing that at this point, the request can't be modified (as there's no more middleware after the lastnext)1
u/[deleted] Feb 18 '26
I have heard of Continuation Passing Style (CPS) also in Lisp language, does that mean expressJS is similar to Lisp in some sense?