manoj kumar
manoj kumar

Reputation: 21

How to pass values and make multiple callbacks in nodejs?

I am a beginner to javascript syntax and I'm trying to understand asynchronous callbacks in nodejs. I'm trying to execute this small piece of code which contains 2 basic functions one after the other.

function compute (n1,n2,callback){
    callback(n1,n2);
}
function sum(n1,n2,callback){
    console.log(n1+n2);
    callback(n1,n2);
}
function product(n1,n2){
    console.log(n1*n2);
}



compute(5, 3, function() {
    sum(function (){
        product();
    });
}); 

I get the following error after I try to run it.

C:\Users\HP\Nodejs\node-example\node-infile-module\practice.js:10
    callback(n1,n2);
    ^

TypeError: callback is not a function
    at sum (C:\Users\HP\Nodejs\node-example\node-infile-module\practice.js:10:5)
    at C:\Users\HP\Nodejs\node-example\node-infile-module\practice.js:19:5
    at compute (C:\Users\HP\Nodejs\node-example\node-infile-module\practice.js:6:5)
    at Object.<anonymous> (C:\Users\HP\Nodejs\node-example\node-infile-module\practice.js:18:1)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

As nodejs supports closure property which automatically uses the parameters from parent functions without needing to explicitly mentioning, I dont understand why it is not considering my function as a callback. Also I want to implement error first approach after I get this basic right. I read other stackoverflow answers but they seemed too complex. Please help me

Upvotes: 1

Views: 657

Answers (3)

Will
Will

Reputation: 3241

You need to pass the arguments through the callbacks.

function compute(n1, n2, callback) {
  callback(n1, n2);
}
function sum(n1, n2, callback) {
  console.log(n1 + n2);
  callback(n1, n2);
}
function product(n1, n2) {
  console.log(n1 * n2);
}

compute(5, 3, function (arg1, arg2) {
  sum(arg1, arg2, function (arg1, arg2) {
    product(arg1, arg2);
  });
});

Upvotes: 0

blex
blex

Reputation: 25659

You need to provide parameters to the sum function (right now, you are passing a function as n1 and that's it) and the product function:

function compute (n1,n2,callback){
    callback(n1,n2);
}
function sum(n1,n2,callback){
    console.log(n1+n2);
    callback(n1,n2);
}
function product(n1,n2){
    console.log(n1*n2);
}
//                     v   v
compute(5, 3, function(n1, n2) {
    sum(n1, n2, product);
    //  ^   ^      ^
});

Upvotes: 1

Wang Liang
Wang Liang

Reputation: 4444

function compute (n1,n2,callback){
    callback(n1,n2);
}
function sum(n1,n2,callback){
    console.log(n1+n2);
    callback(n1,n2);
}
function product(n1,n2){
    console.log(n1*n2);
}

compute(5, 3, function() {
    var arg1 = 5;
    var arg2 = 5;
    // The sum function: type of `third argment` is `function`
    sum(arg1, arg2, function (){
        product();
    });
}); 

Updated

var arg1 = 5;
var arg2 = 5;
sum(arg1, arg2, function (){

Upvotes: 0

Related Questions