Skip to content

Instantly share code, notes, and snippets.

@joynal
Created November 21, 2021 12:48
Show Gist options
  • Save joynal/3c0caef30aa774ff082d435b7df8b7be to your computer and use it in GitHub Desktop.
Save joynal/3c0caef30aa774ff082d435b7df8b7be to your computer and use it in GitHub Desktop.
const job = (state) => new Promise((resolve, reject) => {
if (state) {
resolve('resolved');
} else {
reject('rejected');
}
});
job(true)
.then((data) => {
console.log(data);
return job(true);
})
.then((data) => {
if (data !== 'ok') {
throw 'release the kraken';
}
return job(true);
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
return job(false);
})
.then((data) => {
console.log(data);
return job(true);
})
.catch((error) => {
console.log(error);
return 'error caught';
})
.then((data) => {
console.log(data);
return new Error('all your base are belong to us');
})
.then((data) => {
console.log('success:', data.message);
})
.catch((data) => {
console.log('error:', data.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment