Code with Shahriar
08/01/2024
👩💻 8 HTML Snippets Every Developer Must Know
30/06/2023
What is Callbacks & Callback Hell in JavaScript?
✅Callback
Callback is a function that is passed as an argument to another function.
The main purpose of a callback is to specify what should happen after a particular operation or task is completed.
Example 🌿
Imagine you have a robot named RoboBob, and you
want RoboBob to dance when you clap your hands.
function clapHands (callback) {
}
console.log("You clapped your hands!");
callback(); // This calls the callback function.
function dance() {
}
console.log("RoboBob starts dancing!");
clapHands (dance); //
Give the dance function as the callback to clapHands.
✅Callback hell
Callback hell, also known as the "pyramid of doom," is a term used to describe a situation in JavaScript programming where there are multiple nested callbacks within callbacks, leading to code that becomes hard to read, understand, and maintain.
It typically occurs when working with asynchronous operations that depend on each other or have complex dependencies.
Example🌿
getData(function (data)
{ process1(data, function (result1)
{ process2(result1, function (result)
{ process3 (result2, function (result3) {
// ... more nested callbacks...
});
});
});
});
To address the issue of callback hell, various techniques have emerged in JS such as using Promises, async/await syntax, to handle asynchronous operations more elegantly
Click here to claim your Sponsored Listing.
Category
Website
Address
Sylhet