1

this question is just for curiosity. I know there are other approaches to do the actual thing. I just want to learn some async patterns.

I wanted to search for specific files beginning from a root directory. So I discovered the file module:

https://github.com/mikeal/node-utils/tree/master/file

This module offers a async file.walk(start, callback) function. Where the callback is called for each found directory.

Imagine the case I would like to find all files with a specific name, save the path in an array and do something collaborative processing on them later.

import file = require("file");
var folderArray= []; 
  file.walk(path,function(err,file){
    if(doesExist(file+"/mySpecialFileName")){
      folderArray.push(file);
    }
})
//when all sub directories are searched do something
process(folderArray)

How can I be sure that all directories are searched and I can progress with processing. For example in the dive module: https://github.com/pvorb/node-dive I would just put my process(folderArray) in the third callback which is called when the search is finished.

Thanks a lot. Best P

user6786577
  • 108
  • 1
  • 7
  • To be honest, that library you reference looks like a mess. It is using a mix of synchronous and asynchronous file I/O and thus is not what anyone should be doing. It is not obvious to me that it provides any notification when it is done recursively listing files. You should do this a different way. Either write your own that does it the right way or find a different module that has a better interface with documentation and doesn't use synchronous I/O. – jfriend00 Aug 04 '17 at 17:01
  • Hi, thanks for reply. I am quiet new to async programming and I was just wondering if it is possible with this library. I also wrote know my own method. It took me quiet a long time to figure out that this library is not that good :(. – user6786577 Aug 07 '17 at 06:47

1 Answers1

0

Async Call:Explained here

Callback chaining is one option but it creates Callback Hell so
Use Promise Chaining instead:Promise Exlpained Here

Aniket Jha
  • 1,751
  • 11
  • 13