Skip to content

Instantly share code, notes, and snippets.

@maowug
Last active December 28, 2015 06:45
Show Gist options
  • Save maowug/b7c37e1117258aee2ba5 to your computer and use it in GitHub Desktop.
Save maowug/b7c37e1117258aee2ba5 to your computer and use it in GitHub Desktop.
var koa = require('koa')();
koa.use(function* (next) {
//do something before yielding/passing to next generator function in line which will be 1st event in downstream
console.log("===1===");
console.log("A");
yield next;
// do something when the execution returns upstream, this will be last event in upstream
console.log("B");
console.log("===/1==");
});
koa.use(function* (next) {
// do something before yielding/passing to the next generator function in line, this shall be 2nd event downstream
console.log("====2==");
console.log("C");
yield next;
// do something when the execution returns upstream and this would be 2nd event upstream
console.log("D");
console.log("====/2==");
});
koa.use(function* () { // do something before yielding/passing to next generator function in line. Here it would be last function downstream
console.log("====3==");
console.log("E");
this.body = "hey guys";
console.log("F"); // First event of upstream (from the last to first)
console.log("====/3==");
});
koa.listen(3000);
// ===1===
// A
// ====2==
// C
// ====3==
// E
// F
// ====/3==
// D
// ====/2==
// B
// ===/1==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment