Skip to content

Instantly share code, notes, and snippets.

@thejohnfreeman
Last active January 7, 2019 04:11
Show Gist options
  • Select an option

  • Save thejohnfreeman/00612fd3789e5a7303604d4ef77cce96 to your computer and use it in GitHub Desktop.

Select an option

Save thejohnfreeman/00612fd3789e5a7303604d4ef77cce96 to your computer and use it in GitHub Desktop.
async function () {
const files = await fs.readdir('a');
await Promise.all(files.map(async (filename) => {
const data = await fs.readFile(filename);
await fs.writeFile('b/' + filename, data);
console.log(filename);
}));
}();
// Copy every file in directory `a` to directory `b`.
fs.readdir('a', function (error, files) {
if (error) throw error;
for (var i = 0; i < files.length; ++i) {
var filename = files[i];
fs.readFile(filename, function (error, data) {
if (error) throw error;
fs.writeFile('b/' + filename, data, function (error) {
if (error) throw error;
console.log(filename);
});
});
}
});
async(function*() {
const files = yield fs.readdir('a');
yield Promise.all(files.map(async(function* (filename) {
const data = yield fs.readFile(filename);
yield fs.writeFile('b/' + filename, data);
console.log(filename);
})));
})();
fs.readdir('a')
.then(files => Promise.all(
files.map(filename =>
fs.readFile(filename)
.then(data => fs.writeFile('b/' + filename, data))
.then(() => console.log(filename))
)
))
.catch(error => throw error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment