Bluebird Promise.any() 提前拒绝?

Bluebird Promise.any() early reject?

我在所有 Node.js 项目中都使用 promise 库 Bluebird。为了从文件路径列表中获取第一个现有文件的内容,我使用 Promise.any 成功如下:

Promise.any([
   './foo/file1.yaml',
   './foo/file2.yaml',
   './foo/file3.yaml'
], function(filePath) {
    _readFile(filePath);
}),then(function(fileContent) {
    _console.log(fileContent);
});

我的问题是,如果我在读取文件时遇到不同于 "file not found" 的错误,我该如何提前离开 Promis.any 循环?下面的代码说明了我的问题:

Promise.any([
   './foo/file1.yaml',
   './foo/file2.yaml',
   './foo/file3.yaml'
], function(filePath) {
    _readFile(filePath)
    .catch(function(err) {
       var res = err;
       if (err.code == FILE_NOT_FOUND) {
          // continue the Promise.any loop
       } else {
          // leave the Promise.any loop with this error
          err = new Promise.EarlyBreak(err);
       }
       Promise.reject(err);
    });
}).then(function(fileContent) {
    _console.log(fileContent);
}, function(err) {
    // the first error different from FILE_NOT_FOUND
});

可能是Promise.any不是正确的功能?

提前离开 Promise.any() 循环在概念上是有问题的,因为 Promise.any() 是一个聚合器而不是循环,并且接受一系列承诺,每个承诺都有自己的生命,不确定通过 Promise.any().

然而,从一个路径数组开始,你寻找的循环可以表示为一个 paths.reduce(...) 表达式,它构建一个 .catch() 链,直接如下:

function getFirstGoodFileContent(paths) {
    paths.reduce(function(promise, path) {
        return promise.catch(function() {
            return _readFile(path);
        });
    }, Promise.reject()); // seed the chain with a rejected promise.
}

Catch chain: credit Bergi

由此构建的 .catch 链将在失败时进行下一次迭代,或在成功时跳至链的末尾。这种流量控制与更正常的 .then 链(以已实现的承诺为种子)中发生的情况相反。

但这还不是全部。需要一个额外的条件 - 即 "leave the [Promise.any] loop early if I get an error which is different from 'file not found'"。通过将除 FILE_NOT_FOUND 之外的所有错误发送到成功路径,这非常简单地设计到捕获链中,因此:

  • 影响所需的流量控制(跳过链的其余部分),但是
  • 以错误条件结束成功路线 - 不受欢迎但可以恢复。
function getFirstGoodFileContent(paths) {
    paths.reduce(function(promise, path) {
        return promise.catch(function() {
            return _readFile(path).catch(function(err) {
                if (err.code == FILE_NOT_FOUND) {
                    throw err; // Rethrow the error to continue down the catch chain, seeking a good path.
                } else {
                    return { isError: true, message: err.code }; // Skip the rest of the catch chain by returning a "surrogate success object".
                }
            });
        });
    }, Promise.reject()).then(function(fileContent) {
        // You will arrive here either because :
        // * a good path was found, or
        // * a non-FILE_NOT_FOUND error was encountered.
        // The error condition is detectable by testing `fileContent.isError`
        if (fileContent.isError) {
            throw new Error(fileContent.message); // convert surrogate success to failure.
        } else {
            return fileContent; // Yay, genuine success.
        }
    });
}

所以你现在可以打电话了:

getFirstGoodFileContent([
    './foo/file1.yaml',
    './foo/file2.yaml',
    './foo/file3.yaml'
]).then(function(fileContent) {
    _console.log(fileContent);
}, function(error) {
    // error will be due to :
    // * a non-FILE_NOT_FOUND error having occurred, or
    // * the final path having resulted in an error.
    console.log(error); 
});