Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to figure out the most elegant solution to do the following asynchronously using JavaScript (specifically node):

  • Given a destination file name, check if it is a directory
  • If it is a directory, delete it
  • Copy a source file onto the destination file name

I would like to do this asynchronously using callbacks.

Here is what I have come up with:

  var preCopyOp = fs.lstatSync(dstFilename).isDirectory() ?
    function(callback) {
      rimraf(dstFilename, callback)
    } :
    function(callback) {
      callback();
    };

  preCopyOp(function() {
    ncp(srcFilename, dstFilename, function(err) {
      if(err) {
        return console.error(err);
      }
    });  
  });

The no-op passthrough function seems like a bit of a kludge to me. Is there a more elegant way to do this?

share|improve this question

You can write

function(callback) {
  callback();
}

as just

callback
share|improve this answer
    
Ah yeah of course. Thanks. – JBCP Feb 26 '15 at 3:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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