1

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

3 Answers 3

1

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);
    //  ^   ^      ^
});

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

2 Comments

Why isn't it working if i directly call compute(5,3, sum(...)); instead of enclosing it in a new function with args and then calling it?..
Because when you do compute(5, 3, sum);, you are passing a callback (a function). When you do compute(5, 3, sum(...));, you are calling sum, getting its return value (undefined) and passing it to compute. Enclosing it with an anonymous function makes it possible for compute to call that function and give it parameters
0
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 (){

4 Comments

You forgot to provide parameters to product();
But what is it with the closure property?.. I read that theres no need to explicitly pass params to child functions.. doesn't that mean we can ignore passing args?..
If you pass the name of the function, yes, there is no need: compute(5, 3, sum); This will work fine. But in your example, you create a brand new anonymous function which does not have n1 or n2 declared anywhere in its context
yea i initially tried compute(5,3,sum);. But I want product function to be called as a callback to the sum. Thats where i could'nt figure out how to make multiple callbacks with just passing name of the functions.....
0

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);
  });
});

1 Comment

Why isn't it working if i directly call compute(5,3, sum(...)); instead of enclosing it in a new function with args and then calling it?..

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.