Write An Array of Files in Node
const fs = require('fs');
function writeNextFile(file, content, files, done) {
fs.writeFile(file, content + '\n', () => {
const next = files.shift();
if (!next && done != null) {
done();
return;
}
writeNextFile(next.file, next.content, files, done);
});
}
function writeFiles(files, cb) {
const curr = files.shift();
writeNextFile(curr.file, curr.content, files, cb);
}
writeFiles(
[
{
file: 'zevan1.txt',
content: 'hello world'
},
{
file: 'zevan2.txt',
content: 'goodbye world'
},
{
file: 'confusing.txt',
content: 'greetings world'
}
],
() => {
console.log('done');
}
);
Wrote this snippet for a friend a few weeks back… I don’t do tons of node, so maybe there is a better way but….