Last active
August 29, 2015 14:04
-
-
Save ritch/b4ba46da61bca602c603 to your computer and use it in GitHub Desktop.
Vanilla Express / Express + co comparison
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
// 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