Skip to content

Instantly share code, notes, and snippets.

@pratyushcrd
Created April 7, 2019 20:05
Show Gist options
  • Save pratyushcrd/7012cacbc00078f3aac31040d77c9a30 to your computer and use it in GitHub Desktop.
Save pratyushcrd/7012cacbc00078f3aac31040d77c9a30 to your computer and use it in GitHub Desktop.
callback-hell
function buyBread (input, callback) {
gotoStore(input, (storeErr, storeResponse) => {
if (storeErr) {
return callback(Error('Store is unavailable'), null)
}
buyBrownBread(input, (brownBreadErr, brownBreadResponse) => {
// if unable to buy brown bread, buy white bread
if (brownBreadErr) {
buyWhiteBread(input, (whiteBreadErr, whiteBreadResponse) => {
if (whiteBreadErr) {
return callback(Error('Breads not in stock!'), null)
}
// success
callback(null, whiteBreadResponse)
})
} else {
// success
callback(null, brownBreadResponse)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment