1

I have a nodejs project in which a sample.js include two function function A depends on function B's callback .

// smaple.js
        Both function are included in the same file 
        function B ( arg1 , callback ){
            // do some async
            async.map(arg1 , function(item, cb){
                     var a = item.a
                     var b = a*4
                     cb( null , {'a':a,'b':b})  
                },function(err , result){
                callback(err , result)
            } )


        }


        function A ( arg , callback ){
            if(condition1){
            callback(null , 1)
            }else{
             B ( arg , function( err , result){
                if(err){
                 callback(err , result)
                }
                else if (result ==1 ){

                callback ( null , 2)
                }else{
                callback ( null , 1)
                }

             })
            }
        }

Scenario 2

        // function B is in outer.js and included as module
        sample.js

        var outerfunction = require('./outer.js');

        function A ( arg , callback ){
            if(condition1){
            callback(null , 1)
            }else{
             outerfunction.B ( arg , function( err , result){
                if(err){
                 callback(err , result)
                }
                else if (result ==1 ){

                callback ( null , 2)
                }else{
                callback ( null , 1)
                }

             })
            }
        }


        outer.js


        function B ( arg1 , callback ){
            // do some async
            async.map(arg1 , function(item, cb){
                     var a = item.a
                     var b = a*4
                     cb( null , {'a':a,'b':b})  
                },function(err , result){
                callback(err , result)
            } )


        }

        module.exports.B = B ;

Does scenario 1 is a proper way to use function ? What will happen if this code run concurrently using cluster module?

Does scenario 2 is better way to use dependent function ? Is this code perform better on concurrent execution?

2
  • In your second scenario,since your dependent function is in another module, you have to add your callback function(functionB in outer.js) to module.exports object. Syntax: module.exports.functionB=function() {}
    – user185307
    Jun 29, 2015 at 18:23
  • Have you thought of async- pattern like waterfall ? Oct 28, 2015 at 6:18

1 Answer 1

1

Both cases will do. You just have to maintain the chain of methods to execute.

If you have one method dependent on another method, one of the way's of maintaining the chain is to call the second method inside the callback to the firs one just like you are doing I believe:

Example:

function bar(param) {
    console.log(param);
}

function foo(param, done) {
    if(param === 1) {
        return done(param);
    }
}

foo(1, bar);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.