Skip to content

Instantly share code, notes, and snippets.

@ritch ritch/comparison.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.