Skip to content

Instantly share code, notes, and snippets.

@KoryNunn
Created October 12, 2015 01:04
Show Gist options
  • Select an option

  • Save KoryNunn/57fdb634ecd381c773b0 to your computer and use it in GitHub Desktop.

Select an option

Save KoryNunn/57fdb634ecd381c773b0 to your computer and use it in GitHub Desktop.
A readable version of the foreign async-problem
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