1

It's just a little question about asynchronous function in nodejs and the good way to create them. the following takes in parameters a repository and an extension name and return the list of the files with the given extension in the given repository

to run

$ node app /path/to/some/reposity someExtension

module.js file

//modules.js
var fs = require('fs');
var path = require('path');
module.exports = function(dir, ext, callback){
    //do something
    fs.readdir(dir, function(err, data){
        if(err){
            //EDIT
            process.nexTick(function(){
                callback(err);
            });
            //
        }
        if(data){
            var compteur = 0;
            var block = [];
            for(var i =0 ; i < data.length ; i++){
                if(path.extname(data[i]).slice(1) == ext){
                    block[compteur]= data[i];
                    compteur++;
                }
            }
            //EDIT
            process.nextTick(function(){
                callback(null, block);
            });
            //
        }

    });
};

app.js file

//app.js
var dir = process.argv[2];
var ext = process.argv[3];
var module = require('./module');

module(dir, ext, function(err, data){
    if(err) { throw err;}
    var dl = data.length;
    for(var i = 0 ; i < dl ; i++){
        console.log(data[i]);
    }
});

I just wanted to know if it's the good way to do things or if there a better one. Thanks in advance.

I find this nice tutorial on asynchronous function in NodeJS http://howtonode.org/understanding-process-next-tick

5
  • 1
    That's a pretty standard approach, though you should be aware that the return statements before the callback() calls aren't really doing anything useful. Commented Nov 6, 2013 at 0:33
  • ...and you have an extra closing parenthesis function(err, data)) <-- Commented Nov 6, 2013 at 0:34
  • @BlueSkies if this approach is standard is there any other way to get the same result? For the return statements, I saw callback with and without them, I preferred let them for the moment. Commented Nov 6, 2013 at 0:40
  • doing return callback(..) is a good habit as you might go have a bug where the callback(..) is called twice ! Commented Nov 6, 2013 at 0:42
  • thanks, I just start nodejs few days ago, and had some problem understanding this point Commented Nov 6, 2013 at 0:52

1 Answer 1

2

The standard way to program asynchronously is to use callbacks, which is what you've doing. Since the callback is nested within an asynchronous function, it will be queued once the callstack is empty.

Also, don't name any variables module. It is a global variable which should not be overwritten.

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

3 Comments

thanks, is it better to emit events instead of sending callback or is it similar?
The use case depends on what you're doing. Sometimes one or the either is more convenient to use.
I actually failed to notice that. Thanks for pointing that out. That being said, I shall stop answering questions while lacking sleep.

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.