index.js
function rmDir(p, reserve) {
    if (!isDir(p)) {
        return Promise.resolve(null);
    }
    let deferred = getDefer();
    fs.readdir(p, function (err, files) {
        if (err) {
            return deferred.reject(err);
        }
        let spromise = Promise.resolve(null);
        if (files.length > 0) {
            let promises = files.map(function (item) {
                let filePath = path.normalize(`${p}${path.sep}${item}`);
                if (isDir(filePath)) {
                    return rmDir(filePath, false);
                } else {
                    let defer = getDefer();
                    fs.unlink(filePath, function (er) {
                        return er ? defer.reject(er) : defer.resolve();
                    });
                    return defer.promise;
                }
            });
            spromise = Promise.all(promises);
        }

        return spromise.then(function () {
            if (!reserve) {
                let deferr = getDefer();
                fs.rmdir(p, function (e) {
                    return e ? deferr.reject(e) : deferr.resolve();
                });
                return deferr.promise;
            }
            return Promise.resolve();
        }).then(function () {
            deferred.resolve();
        }).catch(function (e) {
            deferred.reject(e);
        });
    });
    return deferred.promise;
}
function rmDir(p, reserve) {
    if (!isDir(p)) {
        return Promise.resolve(null);
    }
    let deferred = getDefer();
    fs.readdir(p, function (err, files) {
        if (err) {
            return deferred.reject(err);
        }
        let spromise = Promise.resolve(null);
        if (files.length > 0) {
            let promises = files.map(function (item) {
                let filePath = path.normalize(`${p}${path.sep}${item}`);
                if (isDir(filePath)) {
                    return rmDir(filePath, false);
                } else {
                    let defer = getDefer();
                    fs.unlink(filePath, function (er) {
                        return er ? defer.reject(er) : defer.resolve();
                    });
                    return defer.promise;
                }
            });
            spromise = Promise.all(promises);
        }

        return spromise.then(function () {
            if (!reserve) {
                let deferr = getDefer();
                fs.rmdir(p, function (e) {
                    return e ? deferr.reject(e) : deferr.resolve();
                });
                return deferr.promise;
            }
            return Promise.resolve();
        }).then(function () {
            deferred.resolve();
        }).catch(function (e) {
            deferred.reject(e);
        });
    });
    return deferred.promise;
}
function rmDir ( p , reserve ) { if ( ! isDir ( p ) ) { return Promise . resolve ( null ) ; } let deferred = getDefer ( ) ; fs . readdir ( p , function ( err , files ) { if ( err ) { return deferred . reject ( err ) ; } let spromise = Promise . resolve ( null ) ; if ( files . length > 0 ) { let promises = files . map ( function ( item ) { let filePath = path . normalize ( ` ${ p } ${ path . sep } ${ item } ` ) ; if ( isDir ( filePath ) ) { return rmDir ( filePath , false ) ; } else { let defer = getDefer ( ) ; fs . unlink ( filePath , function ( er ) { return er ? defer . reject ( er ) : defer . resolve ( ) ; } ) ; return defer . promise ; } } ) ; spromise = Promise . all ( promises ) ; } return spromise . then ( function ( ) { if ( ! reserve ) { let deferr = getDefer ( ) ; fs . rmdir ( p , function ( e ) { return e ? deferr . reject ( e ) : deferr . resolve ( ) ; } ) ; return deferr . promise ; } return Promise . resolve ( ) ; } ) . then ( function ( ) { deferred . resolve ( ) ; } ) . catch ( function ( e ) { deferred . reject ( e ) ; } ) ; } ) ; return deferred . promise ; }
Subfolders of path p are recursively deleted. When reserve is true, the top-level folder is deleted
Asynchronous mode
@template T
@param {string} p
@param {boolean} reserve
@returns {*}
Subfolders of path p are recursively deleted . When reserve is true the top - level folder is deleted Asynchronous mode