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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.