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