koa.js


Remember: generators

function *() {
var result = yield asyncFunction();
}
Generators are functions that can be paused and resumed
they “yield” values when paused
you can pass values to them when resuming

koa.js

express on steroids generators

https://github.com/koajs/koa


instead of
app.use(function (req, res, next) {
var end = res.end();
res.end = function (…) { // monkey patching
… // a post-filter
res.write(…);
end();
};
next();
});
use generators
app.use(function(next){
  return function *(){
    yield next;
    … // a post filter where it belongs
this.body = …;
 } });


  • Solves the callback pyramid of doom
  • Code flow is a lot clearer
  • No more hung requests by missed res.end()
  • Better error handling with less boilerplate

koa.js

By Arpad Borsos

koa.js

  • 3,271