Last active
April 26, 2018 04:36
-
-
Save gabrielrufino/58073f48db550204b0d340e9d1bc181a to your computer and use it in GitHub Desktop.
Middleware/Pipeline pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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