A JavaScript callback is a function which is to be executed after another function has finished execution.
Simply said:- Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function. This results in callback hell.
// callbacks an callback hell
let fruits = ["apple", "banana", "orange"];
const animateAll = (animate) => {
// below is the callback hell
setTimeout(() => {
animate(fruits[0]);
setTimeout(() => {
animate(fruits[1]);
setTimeout(() => {
animate(fruits[2]);
}, 1000);
}, 1000);
}, 1000);
};
const animate = (fruit) => {
console.log("calling", fruit);
};
animateAll(animate);
calling apple
calling banana
calling orange