2
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You can write

function(callback) {
  callback();
}

as just

callback
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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