I'm trying to wrap my head around the way koajs cascades by awaiting on "next()" before resuming request processing. As far as I can tell, "next" simply refers to the next sequential app.use() in the script.
Can someone more familiar with koajs confirm this? For example, would this code (modified from koajs.com) still work (foo and bar used to be "next")?
app.use(async (ctx, foo) => {
const start = Date.now();
await foo();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
app.use(async (ctx, bar) => {
const start = Date.now();
await bar();
const ms = Date.now() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
});
app.use(async ctx => {
ctx.body = 'Hello World';
});
Or is there something magical about using "next" (besides good semantics)? I'm assuming not, and the behavior is just dependent on the argument order in the function.
If this is the case, then how is it possible to control the flow from one app.use() to another? For example something like this pseudo code:
app.use(async (ctx, a, b) => {
if (!ctx.something) {
await a();
} else {
await b();
}
}
let a = app.use(.....);
let b = app.use(.....);
Any help is appreciated, and also, feel free to point me to an appropriate resource if I've missed something simple.