We usually do some pre-processing on the call request before calling a function, and sometimes we need to do some post-processing on the return value of the function. This creates two invocation business processes. One is sequential invocation, also known as the Onion model (core model), which does both pre – and post-processing. Another type of pure pre-processing is also known as the one-way call model (shell model). These two models are parsed and implemented in detail below.

  • Features of one-way call model (shell model) : function function is located in the outermost layer of the call chain (shell), the return value of the preceding order of the subsequent middleware call, and the output is returned immediately after the preceding order of the function function call.

    MA,MB,MC represent middleware functions and FF represent functional functions

graph TD
INPUT -->MA --> MB -->MC --> FF -->OUTPUT

In this call process, OUTPUT=FF(MC(MB(MA(INPUT)))), at this time, the call direction is the outermost to the kernel, and the function function is located in the outermost of the call.

  • The Onion model (core model) —- Function calls are located in the innermost layer (core layer) Features: The call flow is the sequential middleware calls the subsequent middleware until the function calls and then back.

Sample code:

console.log('pre A start') console.log(ctx) console.log('pre a finish') next(ctx) console.log('after a start') console.log(ctx) console.log('after A finish') } const mB = (ctx,next) => { console.log('pre B start') console.log(ctx) console.log('pre B finish') next(ctx) console.log('after B start') console.log(ctx) console.log('after B finish') } const mC = (ctx,next) => { console.log('pre C start') console.log(ctx) console.log('pre C finish') next(ctx) console.log('after C start') console.log(ctx) console.log('after C finish') } const f1 = (ctx) => { console.log('this is  f1') } const f2 = (ctx) => { console.log('this is f2') } const f3 = (ctx) => { console.log('this is f3') } // let mds =  [mA, mB, mC] // let current=0 function next(ctx) { ctx.current++ if (ctx.current < ctx.mds.length) { ctx.mds[ctx.current](ctx,next) } else { ctx.f(ctx) } } function start(ctx,f) { ctx.current=-1 ctx.mds=[mA, mB, mC] ctx.f=f console.log(ctx.mds) // current = -1; next(ctx) } start({value:5},f2) ` ```Copy the code