简体   繁体   English

Bluebird Promise:any()集合的返回值未定义

[英]Bluebird Promise: any() collection return value undefined

Using bluebird promises, I am trying to check if a certain version of a file exists (always only ONE of those files exists but i don't know which one). 使用蓝鸟诺言,我试图检查文件的某个版本是否存在(始终仅存在其中一个文件,但我不知道哪个文件)。 To speed things up, I am using the any() collection to get all checking out concurrently: 为了加快速度,我使用any()集合来同时进行所有检查:

 Promise.any([
     fs.existsAsync('./foo/image1.jpg'),
     fs.existsAsync('./foo/image2.gif'),
     fs.existsAsync('./foo/image3.png')
 ]).then(function(result) {
     console.log(result); //remains undefined
 });

I am always getting an undefined result. 我总是得到不确定的结果。 According to ( How do I return the response from an asynchronous call? ) this is normal behaviour of a resolved promise. 根据( 我如何从异步调用返回响应? ),这是已解决的Promise的正常行为。 Still, I need to know, which of my three promises resolved or maybe I just can't use any() for that purpose? 不过,我仍然需要知道,我的三个承诺中有哪个已解决,或者也许我不能为此目的使用any()?

The callback for fs.exists() does not follow the expected calling convention of callback(err, value) so it doesn't work with generic promisify. fs.exists()的回调未遵循callback(err, value)的预期调用约定,因此不适用于通用promisify。 It uses just callback(value) . 它仅使用callback(value)

You could either use fs.statAsync() instead or you could create your own promisification for fs.exists() that would work properly. 您可以改用fs.statAsync() ,也可以为fs.exists()创建自己的承诺, fs.exists()正常工作。

Of course, fs.exists() is deprecated anyway for race condition reasons so you should perhaps rethink your tactic here anyway. 当然,由于种族原因,无论如何都建议fs.exists()使用fs.exists()因此无论如何您都应该在这里重新考虑自己的策略。

Here's a properly promisified fs.existsAsync() : 这是一个正确的fs.existsAsync()

fs.existsAsync = function(path) {
    return new Promise(function(resolve) {
        fs.exists(path, resolve);
    });
}

You would assign this after you've done the general promisification of the fs module to replace the wrong fs.existsAsync that Bluebird did automatically. 在完成fs模块的一般承诺后,您将分配此代码,以替换Bluebird自动执行的错误的fs.existsAsync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM