Created
October 12, 2015 01:04
-
-
Save KoryNunn/57fdb634ecd381c773b0 to your computer and use it in GitHub Desktop.
A readable version of the foreign async-problem
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
| var fs = require('fs'), | |
| path = require('path'), | |
| foreign = require('foreign'); | |
| function readFile(filename, callback){ | |
| fs.readFile(filename, {encoding: 'utf8'}, callback); | |
| } | |
| function complete(error, files){ | |
| if(error){ | |
| process.stderr.write(error.message + '\n'); | |
| }else{ | |
| process.stdout.write(files.join('')); | |
| } | |
| process.exit(error ? 1 : 0); | |
| } | |
| function relative(filename){ | |
| return path.join(process.argv[2], filename); | |
| } | |
| function main(){ | |
| readFile(relative('index.txt'), function(error, index){ | |
| if(error) { | |
| return complete(error); | |
| } | |
| foreign.parallel(readFile, index.match(/^.*(?=\n)/gm).map(relative), complete); | |
| }); | |
| } | |
| if (process.argv[1] === __filename) main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment