Last active
January 26, 2018 19:25
-
-
Save AkyunaAkish/0ddf3e53bb4a54ec6f508d29be442d10 to your computer and use it in GitHub Desktop.
Bad Promise Nesting Pattern
This file contains 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
function asyncFunction(bool) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (bool) { | |
resolve('Promise resolved after .5 of a second'); | |
} else { | |
reject('Promise rejected after .5 of a second'); | |
} | |
}, 500); | |
}); | |
} | |
// BAD nesting pattern for Promises | |
asyncFunction(true).then((result) => { | |
asyncFunction(result).then((nestedResult) => { | |
asyncFunction(nestedResult).then((doubleNestedResult) => { | |
asyncFunction(doubleNestedResult).then((tripleNestedResult) => { | |
asyncFunction(tripleNestedResult).then((quadrupleNestedResult) => { | |
console.log('Finally reached the end of the triangle of death: ', quadrupleNestedResult); | |
}) | |
.catch((err) => { | |
console.log('Fifth .catch error: ', err); | |
}); | |
}) | |
.catch((err) => { | |
console.log('Fourth .catch error: ', err); | |
}); | |
}) | |
.catch((err) => { | |
console.log('Third .catch error: ', err); | |
}); | |
}) | |
.catch((err) => { | |
console.log('Second .catch error: ', err); | |
}); | |
}) | |
.catch((err) => { | |
console.log('First .catch error: ', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment