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

2
  • 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. Commented Aug 4, 2017 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 :(. Commented Aug 7, 2017 at 6:47

1 Answer 1

0

Async Call:Explained here

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.