Skip to content

Instantly share code, notes, and snippets.

@manishprajapatidev
Last active May 15, 2020 04:47
Show Gist options
  • Save manishprajapatidev/0bad2f48cfff24bf21a81868894c7aa4 to your computer and use it in GitHub Desktop.
Save manishprajapatidev/0bad2f48cfff24bf21a81868894c7aa4 to your computer and use it in GitHub Desktop.
conditional Chaining Promises
function myPromiseFunction() {
//Change the resolved value to take a different path
return Promise.resolve(true);
}
function conditionalChaining(value) {
if (value) {
//do something
return doSomething().then(doSomethingMore).then(doEvenSomethingMore);
} else {
//do something else
return doSomeOtherThing().then(doSomethingMore).then(doEvenSomethingMore);
}
}
function doSomething() {
console.log("Inside doSomething function");
return Promise.resolve("This message comes from doSomeThing function");
}
function doSomeOtherThing() {
console.log("Inside doSomeOtherthing function");
return Promise.resolve("This message comes from doSomeOtherThing function");
}
function doSomethingMore(message) {
console.log(message);
return Promise.resolve("Leaving doSomethingMore");
}
function doEvenSomethingMore(message) {
console.log("Inside doEvenSomethingMore function");
return Promise.resolve();
}
myPromiseFunction().then(conditionalChaining).then(function () {
console.log("All done!");
}).
catch (function (e) {
});
@apolopena
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment