question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fs.exists doesn't promisify into a good fs.existsAsync

See original GitHub issue

Maybe should be mentioned in documentation.

Problem with promisifyAll of Node’s fs for method fs.existsAsync.

Obvious reason: fs.exists doesn’t callback with an err for first argument, just the result.

Workaround for many uses: Use fs.stat instead.

Example code to illustrate problem:

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

function promise(path) {
    fs.exists(path, function(exists) {
        console.log("callback with path " + path + " exists " + exists);
    });
    fs.stat(path, function(err, stats) {
        if (err) {
            console.log("stat callback with path " + path + " err " + err);
        } else {
            console.log("stat callback with path " + path + " file " + stats.isFile() + ", directory " + stats.isDirectory());
        }
    });
    return fs.existsAsync(path)
    .then(function resolve(exists) {
        console.log("promise with path " + path + " resolves exists " + exists);
    },function reject(reason) {
        console.log("promise with path " + path + " rejects reason " + reason);
    });
}

Promise.try(promise,'/');
Promise.try(promise,'/tmp');
Promise.try(promise,'/somethingimprobable');

with bluebird 2.5.0 (and 2.4.3) produces output

callback with path / exists true
promise with path / rejects reason OperationalError: true
callback with path /tmp exists true
promise with path /tmp rejects reason OperationalError: true
callback with path /somethingimprobable exists false
promise with path /somethingimprobable resolves exists undefined

which means result true gets wrapped as an error.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
srguiwizcommented, Dec 29, 2014

Better workaround:

fs.existsAsync = Promise.promisify
(function exists2(path, exists2callback) {
    fs.exists(path, function callbackWrapper(exists) { exists2callback(null, exists); });
 });

Example code proving it works:

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

fs.existsAsync = Promise.promisify
(function exists2(path, exists2callback) {
    fs.exists(path, function callbackWrapper(exists) { exists2callback(null, exists); });
 });

function promise(path) {
    fs.exists(path, function(exists) {
        console.log("exists callback with path " + path + " exists " + exists);
    });
    return fs.existsAsync(path)
    .then(function resolve(exists) {
        console.log("promise with path " + path + " resolves exists " + exists);
    },function reject(reason) {
        console.log("promise with path " + path + " rejects reason " + reason);
    });
}

Promise.try(promise,'/');
Promise.try(promise,'/tmp');
Promise.try(promise,'/somethingimprobable');

producing output:

exists callback with path / exists true
promise with path / resolves exists true
exists callback with path /tmp exists true
promise with path /tmp resolves exists true
exists callback with path /somethingimprobable exists false
promise with path /somethingimprobable resolves exists false
4reactions
petkaantonovcommented, Dec 28, 2014

FWIW, there should be no reason to use exists, after you have checked that the file exists, the file may disappear before you start to read it and you need the same error handling as you would have needed if you just didn’t use exists in the first place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js check if file exists - fs - Stack Overflow
From Node js documentation, seems like the best way to go if you plan on opening the file after checking its existence, is...
Read more >
Node.js fs.existsSync() Method - GeeksforGeeks
The fs.existsSync() method is used to synchronously check if a file already exists in the given path or not. It returns a boolean...
Read more >
File system | Node.js v19.3.0 Documentation
The fs/promises API provides asynchronous file system methods that return promises. The promise APIs use the underlying Node.js threadpool to perform file ...
Read more >
Node.js — Check If a Path or File Exists - Future Studio
Asynchronously Check if a File Exists in Node.js ... The fs module in Node.js comes with a deprecated exists method. It's recommended not...
Read more >
How to wrap fs.exists() within a Promise | Hiddentao Labs
This makes it easy to convert a Node.js function (which takes a normal callback) ... All good until you decide you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found