Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Promise.all() #16

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions lib/rmdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* Remove all files in the given path recursively.
*/
var fs = require('fs');
var Flow = require('node.flow');

function rmdir(dir, options, callback) {
if (typeof options === 'undefined') {
Expand Down Expand Up @@ -56,32 +55,30 @@ function rmdir(dir, options, callback) {

xfs.lstat(file, function (err, stat) {
function rm_all_dirs(callback) {
var flow = new Flow();

if (!--pending) {
if (!_dirs.length) return callback();

_dirs.forEach(function (dir) {
flow.parallel(function (ready) {
var promises = _dirs.map(function (dir) {
return new Promise(function (resolve, reject) {
xfs.exists(dir, function (exists) {
if (!exists) return ready();
if (!exists) return resolve();

xfs.rmdir(dir, function (err) {
if (err) return ready(err);
if (err) return reject(err);

ready();
resolve();
});
});
});
});

flow.join().
error(function (err) {
if (err) callback(err, _dirs, _files);
}).
end(function () {
callback(null, _dirs, _files);
});
Promise.all(promises)
.then(function () {
callback(null, _dirs, _files);
})
.catch(function (err) {
if (err) callback(err, _dirs, _files);
});
}
}

Expand Down
59 changes: 36 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
{
"name" : "rmdir",
"version" : "1.2.0",
"name": "rmdir",
"version": "1.2.0",
"description": "Remove all files in the given path recursively",
"keywords" : [
"remove", "remove files", "remove all files", "remove path",
"remove dir", "rm -r", "rmdir", "remove files recursively"
"keywords": [
"remove",
"remove files",
"remove all files",
"remove path",
"remove dir",
"rm -r",
"rmdir",
"remove files recursively"
],
"author" : "dreamerslab <[email protected]>",
"author": "dreamerslab <[email protected]>",
"contributors": [
{ "name": "Aaron Larner" },
{ "name": "Glen R. Goodwin" },
{
"name" : "David Pate",
"name": "Aaron Larner"
},
{
"name": "Glen R. Goodwin"
},
{
"name": "David Pate",
"email": "[email protected]",
"url" : "http://davidtpate.com"
"url": "http://davidtpate.com"
},
{
"name" : "Radare",
"name": "Radare",
"email": "[email protected]",
"url" : "http://www.radare.org/"
"url": "http://www.radare.org/"
}
],
"dependencies": {
"node.flow" : "1.2.3"
},
"repository" : {
"repository": {
"type": "git",
"url" : "https://github.com/dreamerslab/node.rmdir.git"
"url": "https://github.com/dreamerslab/node.rmdir.git"
},
"main" : "index",
"engines" : [ "node >= 0.8.0" ],
"licenses": [{
"type": "MIT",
"url" : "http://en.wikipedia.org/wiki/MIT_License"
}],
"scripts" : {
"test" : "node test/run.js"
"main": "index",
"engines": [
"node >= 0.8.0"
],
"licenses": [
{
"type": "MIT",
"url": "http://en.wikipedia.org/wiki/MIT_License"
}
],
"scripts": {
"test": "node test/run.js"
}
}