función de devolución de llamada JS
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
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);
})
function add(a, b, callback) {
if (callback && typeof(callback) === "function") {
callback(a + b);
}
}
add(5, 3, function(answer) {
console.log(answer);
});
method(callback => {
callback.property
})
api(function(result){
api2(function(result2){
api3(function(result3){
// do work
if(error) {
// do something
}
else {
// do something
}
});
});
});
/*
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.