Last active
August 29, 2015 13:58
-
-
Save lleo/9928771 to your computer and use it in GitHub Desktop.
Example use of ES6 generator with 'co' and 'thunkify' modules to read four files in in parallel.
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
#!/usr/bin/env node --harmony-generators | |
var fs = require('fs') | |
, thunkify = require('thunkify') | |
, co = require('co') | |
, readFile = thunkify(fs.readFile) | |
, fns = ["file0.txt", "file1.txt" | |
, "file2.txt", "file3.txt"] | |
co(function*(){ | |
var contents, thunks | |
thunks = fns.map(function(fn){ | |
return readFile(fn) | |
}) | |
contents = (yield thunks).join('') | |
console.log(contents) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment