Skip to content

Instantly share code, notes, and snippets.

@jeveloper
Created March 12, 2015 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeveloper/dad7a61f00d0a3443e2a to your computer and use it in GitHub Desktop.
Save jeveloper/dad7a61f00d0a3443e2a to your computer and use it in GitHub Desktop.
Generators - tiny rewrite
express.use(function(req, res, next){
res.results = new app.results();
var send = function(data){
res.json(data);
};
console.log('are we hijacking?', res.results);
res.results.on('error', send);
res.results.on('fail', send);
res.results.on('success', send);
next();
});
koa.use(function *(next){
//TODO - res.results isnt available here
res.results = new app.results(); //TODO will need to be inject another way
var send = function(data){
res.json(data)
return yield next;
}
res.results.on('error', send);
res.results.on('fail', send);
res.results.on('success', send);
});
//i would probably rewrite results to be independent generators
//TODO one for success
//TODO one for error
//TODO one for fail
//perhaps you'd want to log events somewhere else (stream to log system)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment