how to call a callback function code example
Example 1: call back function in javascript
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Example 2: javascript callback
function newCallback(callback) {
callback('This can be any value you want to return')
}
function actionAferCallback (callbackData) {
console.log(callbackData)
}
function requestCallback() {
newCallback(actionAferCallback)
}
Example 3: what are callbacks
names = ['Anthony', 'Betty', 'Camie']
function addName(name, callback){
setTimeout(function(){
names.append(name)
callback()
}, 200)
}
function getNames(){
setTimeout(function(){
console.log(names)
}, 100)
}
addName('Daniel', getNames)
Example 4: js callback function
const add = (num1, num2) => num1 + num2;
const result = (num1, num2, cb) => {
return "result is:" + cb(num1, num2);
}
const res = result(12, 13, add);