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?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Feb 11, 2015 at 20:48
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You can write

function(callback) {
 callback();
}

as just

callback
answered Feb 26, 2015 at 2:40
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.