Skip to content

Instantly share code, notes, and snippets.

@tomshen
Created January 22, 2014 01:12
Show Gist options
  • Save tomshen/8551814 to your computer and use it in GitHub Desktop.
Save tomshen/8551814 to your computer and use it in GitHub Desktop.
Demonstrating three different ways to delete files whose names are stored in an array.
var companies = [/* ... */];
/* WITH SYNC */
var fs = require("fs");
companies.forEach(fs.unlinkSync(company)); // this is blocking
console.log("All files deleted");
/* WITH CALLBACKS */
var fs = require("fs");
var unlinkCount = companies.length;
companies.forEach(function (company) {
fs.unlink(company, function () {
unlinkCount--;
if (unlinkCount === 0)
console.log("All files deleted");
});
});
/* WITH PROMISES */
var fs = require("q-io/fs");
var Promise = require("q");
var companyDeletionPromises = companies.map(fs.remove(company));
Promise.all(companyDeletionPromises).done(function () {
console.log("All files deleted");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment