2

Assume I have an array with paths to multiple files. I would like to delete these files asynchronously.

var files = ['file1.txt', 'file2.txt']; fs.unlink(..., callback()) 

I came across with this solution Delete several files in node.js but I think it violates node.js practices (asynchronous function inside a for loop). Is there any other, better solution for this?

4
  • Why would an asynchronous function in a loop violate any nodejs practices? What do you mean by "better"? Commented May 23, 2015 at 21:25
  • Thank you for your time. @Bergi in a for loop the function launches all the fs.unlink on the background, not leaving them a chance to finish. In my opinion that is ugly, isn't it? Commented May 23, 2015 at 21:39
  • 1
    @VladMatvei: "leaving them a chance to finish" before what? Launching them in the background (unlike fs.unlinkSync) is desired, as it speeds up your script by processing them in parallel. Commented May 23, 2015 at 21:42
  • 1
    @VladMatvei The point of asynchronicity is to not to wait till a IO/network operation completes.We should only be worried about how we will get to know once the operation completes. One option is callback. But then we need to explicitly keep track of number of times the callbacks are called. That would make the code highly unreadable and unmaintainable. Commented May 23, 2015 at 21:43

3 Answers 3

2

If you want to run a an arbitrary list of tasks (unlinking files) asynchronously, but know when they are all done, you can use the async.js module. It can run tasks in series and parallel.

So push all your unlink function calls into an array, then call async.parallel() and let them fly. Then when they are all done, you land in a single manageble callback.

var files = ['file1.txt', 'file2.txt']; var myParallelTasks = []; files.forEach( function( fileName ) { myParallelTasks.push( fs.unlink( fileName, function(callback) { callback(); }) ); } async.parallel( myParallelTasks, function() { // all done console.log( "all done" ); }); 
Sign up to request clarification or add additional context in comments.

Comments

1

Try the option of recursion (code from your link in the question):

function deleteFiles(files, callback){ var i = files.length; var file = files.pop(); if ( file == undefined ) { callback(); } else { // do the unlinking ... deleteFiles(files, callback); } } 

Comments

0
async deleteAll(filePathsList) { try { await this.deleteFiles(filePathsList); logger.log('info', "Following files deleted successfully from EFS --> " + filePathsList.toString()); return true; } catch (error) { logger.log('error', error.stack || error); logger.log('error', "Error occured while deleting files from EFS"); return false; } } async deleteFiles(files) { return new Promise((resolve, reject) => { let i = files.length; files.forEach(function(filepath) { fs.unlink(filepath, function(err) { i--; if (err && err.code == 'ENOENT') { // file doens't exist logger.log('info', "Following file doesn't exist, So won't be deleted-->" + (filepath || '')); } else if (err) { // other errors, e.g. maybe we don't have enough permission logger.log('error', "Error occured while deleting the file " + (filepath || '') + " due to error" + err); reject(err); return; } else if (i <= 0) { resolve(); } }); }); }) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.