fs.exists doesn't promisify into a good fs.existsAsync
See original GitHub issueMaybe 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:
- Created 9 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Better workaround:
Example code proving it works:
producing output:
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.