var fs = require('fs');
/**
 * @return {object} Promise
 */
function doThing(fileName) {
   // ...
   // console.log(fileName);
   // do something and return a promise
}

/**
 * @return {object} Promise
 */
function walk(fileName, cb) {
  var pList = [];
  var states = fs.statSync(fileName);
  if (states.isDirectory()) {
    var files = fs.readdirSync(fileName);
    files.forEach(function(file) {
      pList.push(walk(fileName + '/' + file, cb));
    });
  } else if (states.isFile()) {
    pList.push(cb(fileName));
  }
  return Promise.all(pList);
}

walk('filename', doThing).then(function() {
  console.log('done');
}).catch(function(err) {
  console.log(err);
});

原地址:http://cnodejs.org/topic/568dc05ac2289f51658f0856

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2021-11-13
  • 2021-05-02
  • 2021-07-02
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
猜你喜欢
  • 2021-05-03
  • 2021-05-26
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案