xxxxxxxxxx
/*
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.
xxxxxxxxxx
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
xxxxxxxxxx
// What is callback functions in js
// Lets make a function
// Putting parameter in it "callback"
function func1(callback) {
console.log(callback); // Log 'callback'; And now our parameter callback is a function and we can call it from our func1 and this is what callback is
hello();
}
// Making one more function to print hello world in console
function hello() {
console.log("Hello world");
}
func1(hello); // Call func and give hello function as a arguement; Yes we can give function as arguement
// func1(hello()); // This is wrong way to give function as arguement
xxxxxxxxxx
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback , {
var name = prompt('Please enter your name.');
callback(name);
}}
processUserInput(greeting);
xxxxxxxxxx
// A function that accepts a callback function as an argument
function performTask(callback) {
// Simulating an asynchronous task
setTimeout(function() {
// Perform some task
console.log('Task completed');
// Execute the callback function
callback();
}, 2000);
}
// Define the callback function
function afterTaskCompletion() {
console.log('Callback function executed');
}
// Call the performTask() function and pass the callback
performTask(afterTaskCompletion);
xxxxxxxxxx
function sum(num1,num2,callback){
let total = num1 + num2
callback(total)
}
sum(10,20,function(total){
// received total here
console.log(total);
})
sum(5,16,(total)=>{
console.log(total);
})
xxxxxxxxxx
// Example using callback in JavaScript
function greetUser(name, callback) {
console.log("Hello, " + name);
callback();
}
function finishGreeting() {
console.log("Greeting finished.");
}
// Usage
greetUser("John", finishGreeting);
xxxxxxxxxx
const orderCoffee = callback => {
let coffee = null;
console.log("Sedang membuat kopi, silakan tunggu...");
setTimeout(() => {
coffee = "Kopi sudah jadi!";
callback(coffee);
}, 3000);
}
orderCoffee(coffee => {
console.log(coffee);
});
/* output
Sedang membuat kopi, silakan tunggu...
---- setelah 3 detik ----
Kopi sudah jadi!
*/
xxxxxxxxxx
function processData(data, callback) {
// Perform some processing on the data
let processedData = data.toUpperCase();
// Call the callback function and pass the processed data
callback(processedData);
}
function displayData(data) {
console.log(data);
}
// Calling the processData function with the displayData function as a callback
processData("hello world", displayData);
xxxxxxxxxx
api(function(result){
api2(function(result2){
api3(function(result3){
// do work
if(error) {
// do something
}
else {
// do something
}
});
});
});