1
\$\begingroup\$

I hate to have to write a custom exists promise every time I need to know if a file exists, so I came up with this. Thoughts?

var Promise = require("bluebird")
var fs = Promise.promisifyAll(require("fs"))

function fsExists(_path){
  return fs.lstatAsync(_path)
  .then(function(){ return true })
  .catch(function(){ return false })
}

module.exports = fsExists
\$\endgroup\$
1
\$\begingroup\$

I think the catch and then parts are unnecessary. A then accepts a resolve and reject handler, which you can treat a boolean.

var Promise = require("bluebird")
var fs = Promise.promisifyAll(require("fs"))

module.exports = fs.lstatAsync

Usage would simply be

var stat = require('yourFsStatModule');

stat('path/to/file').then(function(){
  // it exists
}, function(){
  // it doesn't exist
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.