Skip to content

Instantly share code, notes, and snippets.

@ritch
Last active August 29, 2015 14:04
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 ritch/b4ba46da61bca602c603 to your computer and use it in GitHub Desktop.
Save ritch/b4ba46da61bca602c603 to your computer and use it in GitHub Desktop.
Vanilla Express / Express + co comparison
// my existing express app
app.use(myMiddleware);
function myMiddleware(req, res, next) {
doSomething(next); // does something async + takes a callback
}
app.get('/', function(req, res, next) {
getHomePageData(function(err, data) {
if(err) return next(err);
res.render('home', {data: data});
});
});
// co-routine-ified
app.use(myMiddleware);
function* myMiddleware(req, res, next) {
var err;
try {
yield doSomething(); // does something async + returns a promise or thunk
} catch(e) {
err = e;
}
next(err);
}
app.get('/', function(req, res, next) {
co(function* () {
var data = yield getData(); // returns a thunk or promise
res.render('home', {data: data});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment