What is callback in javaScript

Callback in JavaScript

Callback is also called the higher order function in javaScript.

 

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

 function greeting(name) {   
alert('Hello ' + name); 
}  

function processUserInput(callback) {   
var name = prompt('Please enter your name.');   
callback(name); 
}  

processUserInput(greeting); 

The above example is a synchronous callback, as it is executed immediately.

 let x = function(){ 
console.log("I am called from inside a function"); 
}; 

let y = function(callback){ 
console.log("Do something"); 
callback(); 
}; 

y(x); 

Multiple callabcks

 let add = function(a,b){ 
return a+b; 
}; 

let multiply = function(a,b){ 
return a*b; 
}; 

let substraction = function(a,b){ 
return a-b; 
} 

let calculate = function(num1,num2,callback){ 
return callback(num1,num2); 
} 

console.log(calculate(5,3,add)); 

Anonymous callback function

 let calculate = function(num1,num2,callback){ 
return callback(num1,num2); 
} 

console.log(calculate(5,3,function(a,b){ 
return a*b; 
})); 

How To Set Up a Multi-Node Kafka Cluster using KRaft

Setting up a multi-node Kafka cluster using KRaft (Kafka Raft) mode involves several steps. KRaft mode enables Kafka to operate without the need for Apache ZooKeeper, streamlining the architecture and improving management. Here’s a comprehensiv …

read more

Streamline Data Serialization and Versioning with Confluent Schema Registry …

Using Confluent Schema Registry with Kafka can greatly streamline data serialization and versioning in your messaging system. Here's how you can set it up and utilize it effectively: you can leverage Confluent Schema Registry to streamline data seria …

read more