Last active
May 15, 2020 04:47
-
-
Save manishprajapatidev/0bad2f48cfff24bf21a81868894c7aa4 to your computer and use it in GitHub Desktop.
conditional Chaining Promises
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 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) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!