javascript - bluebird - 函数返回 promise 对象而不是实际数据

标签 javascript node.js bluebird

正在关注 this snippet我正在尝试编写一个循环遍历目录、查找目录并从这些目录中读取 xml 文件名的函数(我知道文件夹结构将始终保持不变)。到目前为止,我的函数按预期工作,但是当我尝试从函数中获取返回值时,我只获取了 Promise 对象。

我的代码:

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

function getFileNames(rootPath) {
  // Read content of path
  return fs.readdirAsync(rootPath)
    // For every file in path
    .then(function(directories) {
      // Filter out the directories
      return directories.filter(function(file) {
        return fs.statSync(path.join(rootPath, file)).isDirectory();
      });
    })
    // For every directory
    .then(function(directories) {
      return directories.map(function(directory) {
        // Read file in the directory
        return fs.readdirAsync(path.join(rootPath, directory))
          .filter(function(file) {
            return path.extname(file) == '.XML';
          })
          .then(function(files) {
            // Do something with the files
            return files;
          });
        });
    });
}

getFileNames('./XML').then(function(files) {
  console.log(files);
});

当我在 getFileNames 中的最后一个 .then 函数中 console.log(files) 时,我得到了实际的文件名数组控制台。但是当我运行上面的代码时,我得到了这个输出:

[ Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined },
  Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined },
  Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined },
  Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined },
  Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined } ]

为什么会发生这种情况以及如何解决?

最佳答案

行内

.then(function(directories) {
  return directories.map(function(directory) {
    return fs.readdirAsync…

您正在为一系列 promise 创建一个 promise ,而这正是您在最终日志中获得的内容。您不需要返回一组 promise ,而是需要返回一组值的 promise - 和 Promise.all正是这样做的:

.then(function(directories) {
  return Promise.all(directories.map(function(directory) {
    return fs.readdirAsync(…)
    …
  }));
})

但是,在 Bluebird 中,使用 Promise.map(directories, function(…) { … }) 会更加惯用甚至 the map method (类似于您已经使用 .filter 对每个目录中的文件执行的操作):

function getFileNames(rootPath) {
  return fs.readdirAsync(rootPath)
  .filter(function(file) {
    return fs.statAsync(path.join(rootPath, file)).then(function(s) {
      return s.isDirectory();
    });
  })
  .map(function(directory) {
//^^^^
    return fs.readdirAsync(path.join(rootPath, directory))
    .filter(function(file) {
      return path.extname(file) == '.XML';
    })
    .map(function(file) {
      // Do something with every file
      return file;
    });
  });
}

关于javascript - bluebird - 函数返回 promise 对象而不是实际数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693843/

相关文章:

来自导入数组的 JavaScript 随机项行为异常

javascript - 如何防止粘性 header 以小分辨率运行

node.js - 检查当前 Node 版本

javascript - 从 bluebird Promise.each() 中调用异步函数

javascript - 如果文本太长,如何用空格分隔文本?

javascript - 为什么单击一个表单元素会将焦点设置到另一个?

mysql - 在 sequelize 中关联两个表

javascript - async await node.js 跳过第二个 await 语句保存 mongodb

javascript - NodeJS Promises 无法编辑现有的 'model'

javascript - Bluebird 的 promise 和事件