Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
co + thunkify http servers
// parallel steps
var co = require('co');
var http = require('http');
var fs = require('fs');
var thunkify = require('thunkify');
var server = http.createServer(co(function *(req, res) {
var files = yield {
a: thunkify(fs.readFile)('a.txt'),
b: thunkify(fs.readFile)('b.txt'),
c: thunkify(fs.readFile)('c.txt')
};
res.end(files.a + files.b + files.c);
}));
server.listen(5000);
// sequential steps
var co = require('co');
var http = require('http');
var fs = require('fs');
var thunkify = require('thunkify');
var server = http.createServer(co(function *(req, res) {
var a = yield thunkify(fs.readFile)('a.txt');
var b = yield thunkify(fs.readFile)('b.txt');
var c = yield thunkify(fs.readFile)('c.txt');
res.end(a + b + c);
}));
server.listen(5000);
@wryk

This comment has been minimized.

Copy link

wryk commented Mar 31, 2014

@substack

This comment has been minimized.

Copy link
Owner Author

substack commented Mar 31, 2014

@wryk koa has too many weird opinions for my taste, like assigning onto this.body to set the response. And middleware.

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.