samedi 7 mars 2015

How to know non blocking Recursive job is complete in nodejs

I have written this non-blocking nodejs sample recursive file search code, the problem is I am unable to figure out when the task is complete. Like to calculate the time taken for the task.



fs = require('fs');

searchApp = function() {

var dirToScan = 'D:/';

var stringToSearch = 'test';

var scan = function(dir, done) {
fs.readdir(dir, function(err, files) {
files.forEach(function (file) {
var abPath = dir + '/' + file;
try {
fs.lstat(abPath, function(err, stat) {
if(!err && stat.isDirectory()) {
scan(abPath, done);;
}
});
}
catch (e) {
console.log(abPath);
console.log(e);
}

matchString(file,abPath);
});
});
}

var matchString = function (fileName, fullPath) {
if(fileName.indexOf(stringToSearch) != -1) {
console.log(fullPath);
}
}

var onComplte = function () {
console.log('Task is completed');
}

scan(dirToScan,onComplte);
}


searchApp();


Above code do the search perfectly, but I am unable to figure out when the recursion will end.


Aucun commentaire:

Enregistrer un commentaire