1

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:

 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. Still, I need to know, which of my three promises resolved or maybe I just can't use any() for that purpose?

Community
  • 1
  • 1
Igor P.
  • 1,407
  • 2
  • 20
  • 34

1 Answers1

1

The callback for fs.exists() does not follow the expected calling convention of callback(err, value) so it doesn't work with generic promisify. It uses just callback(value).

You could either use fs.statAsync() instead or you could create your own promisification for fs.exists() that would work properly.

Of course, fs.exists() is deprecated anyway for race condition reasons so you should perhaps rethink your tactic here anyway.

Here's a properly promisified 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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979