bluebird - fs.readdir().then - 无法读取未定义的 属性 then

bluebird - fs.readdir().then - cannot read propery then of undefined

我正在尝试承诺 fs readdir function using bluebird。我只想读取一个目录下的所有文件,然后输出console.log.

的数组
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));

fs.readdir('./XML').then(function(err, directories) {
  console.log(directories);
});

我得到以下输出:

fs.readdir('./XML').then(function(err, directories) { ^

TypeError: Cannot read property 'then' of undefined at Object. (/Users/shooshte/Sportradar/notThatKindOfPeon/bluebird.js:4:20) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3

我做错了什么?

似乎库将 "Async" 添加到方法名称中: 它对我来说是这样的:

const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));

fs.readdirAsync('./XML').then(function(directories) {
  console.log(directories);
});
fs.readdirAsync('./XML').then()

Documentation, second sentence:

The promisified method name will be the original method name suffixed with suffix (default is "Async").

您可能只想承诺要使用的函数:

const readdir = require('bluebird').promisify(require('fs').readdir);

return readdir(dir).then(function(err, files) {
  console.log('FILES', files)
  return files;
});