Skip to content

Instantly share code, notes, and snippets.

@niinpatel
Last active June 9, 2018 18:39
Show Gist options
  • Save niinpatel/7fe0feb83816ee658533a846eec30aa4 to your computer and use it in GitHub Desktop.
Save niinpatel/7fe0feb83816ee658533a846eec30aa4 to your computer and use it in GitHub Desktop.
An example of a callback hell
/*
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