Last active
June 9, 2018 18:39
-
-
Save niinpatel/7fe0feb83816ee658533a846eec30aa4 to your computer and use it in GitHub Desktop.
An example of a callback hell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Suppose I want to add two numbers, then double it, then square that number, then add double it again. | |
*/ | |
addTwoNos(2, 3, (sum) => { | |
double(sum, (doubleOfSum) => { | |
square(doubleOfSum, (square) => { | |
double(square, (doubleOfSquare) => { | |
console.log(doubleOfSquare) | |
}) | |
}) | |
}) | |
}); // outputs result of (((2+3)*2)^2)*2 which is 200 | |
function addTwoNos(num1, num2, callback) { | |
callback(num1 + num2) | |
} | |
function double(num, callback) { | |
callback(num * 2) | |
} | |
function square(num, callback) { | |
callback(num * num) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment