Last active
December 28, 2015 06:45
-
-
Save maowug/b7c37e1117258aee2ba5 to your computer and use it in GitHub Desktop.
how koajs works @cc http://code.tutsplus.com/tutorials/introduction-to-generators-koajs-part-1--cms-21615
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
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