Skip to content

Instantly share code, notes, and snippets.

@AkyunaAkish
Last active January 26, 2018 19:25
Show Gist options
  • Save AkyunaAkish/0ddf3e53bb4a54ec6f508d29be442d10 to your computer and use it in GitHub Desktop.
Save AkyunaAkish/0ddf3e53bb4a54ec6f508d29be442d10 to your computer and use it in GitHub Desktop.
Bad Promise Nesting Pattern
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