Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save invalidred/98307b909801e1c8c9014b4eb94de451 to your computer and use it in GitHub Desktop.
Save invalidred/98307b909801e1c8c9014b4eb94de451 to your computer and use it in GitHub Desktop.
Javascript Promises Conundrum
const getResolvedPromise = () => Promise.resolve('resolved');
const getRejectPromise = () => Promise.reject('rejected');
getResolvedPromise()
//getRejectPromise()
.then(res => {
console.log('in the 1st then');
//return getResolvedPromise();
return getRejectPromise();
}, error => {
console.log(`error 1 ${error}`);
throw error;
})
.then(() => {
console.log('in the 2nd then');
//return getResolvedPromise();
return getRejectPromise();
}, error => {
console.log(`error 2 ${error}`);
})
.then(() => {
console.log('in the 3rd then');
//return getRejectPromise();
return getResolvedPromise();
}, error => {
console.log(`error 3 ${error}`);
})
.then(() => {
console.log('in the 4th then');
return getResolvedPromise();
//return getRejectPromise();
}, error => {
console.log(`error 4 ${error}`);
return getRejectPromise();
})
.catch(error => console.log(`catch: ${error}`));
const getResolvedPromise = () => Promise.resolve('resolved');
const getRejectPromise = () => Promise.reject('rejected');
const getRejectPromise2 = () => {
return new Promise((resolve, reject) => {
reject('rejected 2');
});
};
const getRejectPromise3 = () => {
return new Promise((resolve, reject) => {
throw('rejected 3');
});
};
const promise1 = getRejectPromise()
.then(res => console.log('in the 1st then'))
.catch(error => console.log(`error 1 ${error}`));
const promise2 = promise1
.then(res => {
console.log('in the 2nd then');
return getRejectPromise3();
//return getResolvedPromise();
})
.catch(error => console.log(`error 2 ${error}`));
const promise3 = promise2
.then(res => {
console.log('in the 3rd then');
return getResolvedPromise();
})
.catch(error => console.log(`error 3 ${error}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment