42
loading...
This website collects cookies to deliver better user experience
"I will call back later!"
A callback is a function passed as an argument to another function
This technique allows a function to call another function
A callback function can run after another function has finished
// A function
function fn() {
console.log('Just a function');
}
// A function that takes another function as an argument
function higherOrderFunction(callback) {
// When you call a function that is passed as an argument, it is referred to as a callback
callback();
}
// Passing a function
higherOrderFunction(fn);
function callback(e) {
alert(e.target.innerText);
}
document.getElementById("name").addEventListener("click", callback);
function handleLog(posts) {
console.log(JSON.stringify(posts));
}
const fetchData = (handleLog) => {
fetch("http://localhost:3000/posts")
.then(res => res.json())
.then(posts => handleLog(posts));
};
fetchData(handleLog);
function callback() {
console.log("this => callback function", this);
};
function handleThis(callback) {
this.address = "thaibinh";
console.log("this => internal function", this);
callback();
};
handleThis(callback);
function callback() {
console.log("this => callback function", this);
};
function handleThis(callback) {
const obj = {
name: "chamdev.com",
callbackObj = callback(),
};
console.log("this => internal function", this);
obj.callbackObj();
};
handleThis(callback);
function callback() {
console.log("this => callback function", this);
};
function handleThis(callback) {
new callback();
};
handleThis(callback);
const makeBurger = nextStep => {
getBeef(function (beef) {
cookBeef(beef, function (cookedBeef) {
getBuns(function (buns) {
putBeefBetweenBuns(buns, beef, function(burger) {
nextStep(burger);
});
});
});
});
};