Advertisement
vinissh

middleware1.js

Jan 18th, 2021 (edited)
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //middleware parttner (chain of responsability)
  2.  
  3. const passo1 = ( ctx, next) =>{
  4.     ctx.valor1 = 'mid1'
  5.     next()
  6. }
  7.  
  8.  
  9. const passo2 = (ctx, next) =>{
  10.     ctx.valor2 = 'mid'
  11.     next()
  12. }
  13.  
  14. const passo3 = ctx => ctx.valor3 = 'mid3'
  15.  
  16. const exec = (ctx, ...midlewares) =>{
  17.     const execPassso = indice =>{
  18.         midlewares && indice < midlewares.length &&
  19.             midlewares[indice](ctx, () => execPassso(indice + 1))
  20.     }
  21.     execPassso(0)
  22. }
  23.  
  24. const ctx  = {}
  25. exec(ctx, passo1, passo2, passo3)
  26. console.log(ctx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement