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?