Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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?

share|improve this question
    
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() {} –  Pramod Kumar Jun 29 at 18:23

1 Answer 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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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