Created
June 6, 2013 00:14
'Co' for nodejs
This file contains 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
var co = require('..'); | |
var join = co.join; | |
var fs = require('fs'); | |
function size(file) { | |
return function(fn){ | |
fs.stat(file, function(err, stat){ | |
if (err) return fn(err); | |
fn(null, stat.size); | |
}); | |
} | |
} | |
// 3 concurrent stat()s at a time | |
co(function *(){ | |
var a = yield join(size('.gitignore'), size('index.js'), size('Makefile')); | |
var b = yield join(size('.gitignore'), size('index.js'), size('Makefile')); | |
var c = yield join(size('.gitignore'), size('index.js'), size('Makefile')); | |
console.log(a); | |
console.log(b); | |
console.log(c); | |
}); | |
// 9 concurrent stat()s | |
co(function *(){ | |
var a = join(size('.gitignore'), size('index.js'), size('Makefile')); | |
var b = join(size('.gitignore'), size('index.js'), size('Makefile')); | |
var c = join(size('.gitignore'), size('index.js'), size('Makefile')); | |
var d = yield join(a, b, c); | |
console.log(d); | |
}); | |
// 3 | |
co(function *(){ | |
var a = size('.gitignore'); | |
var b = size('index.js'); | |
var c = size('Makefile'); | |
var res = yield join(a, b, c); | |
console.log(res); | |
}); |
This file contains 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
var co = require('..'); | |
var fs = require('fs'); | |
function size(file) { | |
return function(fn){ | |
fs.stat(file, function(err, stat){ | |
if (err) return fn(err); | |
fn(null, stat.size); | |
}); | |
} | |
} | |
var foo = co(function *(){ | |
var a = yield size('.gitignore'); | |
var b = yield size('Makefile'); | |
var c = yield size('package.json'); | |
return [a, b, c]; | |
}) | |
var bar = co(function *(){ | |
var a = yield size('examples/parallel.js'); | |
var b = yield size('examples/nested.js'); | |
var c = yield size('examples/simple.js'); | |
return [a, b, c]; | |
}) | |
co(function *(){ | |
var a = yield foo; | |
var b = yield bar; | |
console.log(a); | |
console.log(b); | |
}) |
This file contains 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
var co = require('..'); | |
var fs = require('fs'); | |
var read = co.wrap(fs.readFile); | |
var sizes = co(function *(){ | |
var a = yield read('.gitignore'); | |
var b = yield read('Makefile'); | |
var c = yield read('package.json'); | |
return [a.length, b.length, c.length]; | |
}); | |
sizes(function(err, res){ | |
console.log(res); | |
}); |
This file contains 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
var co = require('..'); | |
var fs = require('fs'); | |
function read(file) { | |
return function(fn){ | |
fs.readFile(file, 'utf8', fn); | |
} | |
} | |
co(function *(){ | |
var a = yield read('.gitignore'); | |
var b = yield read('Makefile'); | |
var c = yield read('package.json'); | |
console.log(a); | |
console.log(b); | |
console.log(c); | |
}) |
This file contains 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
var co = require('..'); | |
var fs = require('fs'); | |
var read = co.wrap(fs.readFile); | |
co(function *(){ | |
var a = yield read('.gitignore'); | |
var b = yield read('Makefile', 'ascii'); | |
var c = yield read('package.json', 'utf8'); | |
console.log(a); | |
console.log(b); | |
console.log(c); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment