Skip to content

Instantly share code, notes, and snippets.

@monjer
Created July 17, 2020 08:27
Show Gist options
  • Select an option

  • Save monjer/811c286ec37d05f58de204ed3fd6d00a to your computer and use it in GitHub Desktop.

Select an option

Save monjer/811c286ec37d05f58de204ed3fd6d00a to your computer and use it in GitHub Desktop.
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
//
// 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