Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
concat using promises
var when = require('when');
var fs = require('fs');
var concat = {};
concat.each = function(src, dest, options) {
if (!options) { options = {}; }
if (!options.separator) { options.separator = ''; }
return when.reduce(src, function(output, srcpath) {
var dfd = when.defer();
fs.readFile(srcpath, function(err, data) {
if (err) { dfd.reject(err); }
dfd.resolve(output.concat(data));
});
return dfd;
}, []).then(function(combined) {
var dfd = when.defer();
fs.writeFile(dest, combined.join(options.separator), function(err) {
if (err) { dfd.reject(err); }
dfd.resolve('File written.');
});
return dfd;
});
};
concat.each(
['test/fixtures/concat/a.txt', 'test/fixtures/concat/b.txt', 'test/fixtures/concat/c.txt'],
'tmp/abc.txt',
{separator: '\n'}
).then(function(status) {
console.log(status);
});
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.