Skip to content

Instantly share code, notes, and snippets.

@gabrielrufino
Last active April 26, 2018 04:36
Show Gist options
  • Save gabrielrufino/58073f48db550204b0d340e9d1bc181a to your computer and use it in GitHub Desktop.
Save gabrielrufino/58073f48db550204b0d340e9d1bc181a to your computer and use it in GitHub Desktop.
Middleware/Pipeline pattern
const generator = () => {
const middlewares = []
const use = (fn) => middlewares.push(fn)
const runMiddleware = index => {
if (index < middlewares.length) {
middlewares[index](() => runMiddleware(index + 1))
}
}
const run = () => {
runMiddleware(0)
console.log('The end!')
}
return {
run,
use
}
}
module.exports = generator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment