Created
July 17, 2020 08:27
-
-
Save monjer/811c286ec37d05f58de204ed3fd6d00a to your computer and use it in GitHub Desktop.
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
| plugins.use(async (next) => { | |
| console.log('a'); | |
| await next(); | |
| console.log('b') | |
| }) | |
| plugins.use(async (next) => { | |
| console.log('c'); | |
| await next(); | |
| console.log('d') | |
| }) | |
| plugins.run(() => { | |
| console.log('done') | |
| }); | |
| // output a c d b |
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
| // | |
| // Simple plugins mode in koajs use JavaScript async await | |
| // https://github.com/senchalabs/connect/blob/master/index.js | |
| // | |
| const plugins = { | |
| stack: [], | |
| use: (plugin) => { | |
| plugins.stack.push(plugin); | |
| }, | |
| run: async (done) => { | |
| const { stack } = plugins; | |
| let index = 0; | |
| const next = async () => { | |
| const plugin = stack[index++]; | |
| if (!plugin) { | |
| return Promise.resolve(); | |
| } | |
| await plugin(next); | |
| } | |
| await next(); | |
| done && done() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment