Last active
March 6, 2017 19:57
-
-
Save danielbdias/495829c9909aa9509673364a8824f9ce to your computer and use it in GitHub Desktop.
To set promiseChain or not to set promiseChain, that is the question
This file contains hidden or 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
const printablePromise = text => | |
new Promise(resolve => { | |
console.log(text) | |
return resolve() | |
}) | |
const actionA = () => printablePromise('A') | |
const actionB = () => printablePromise('B') | |
const actionC = () => printablePromise('C') | |
const actionD = () => printablePromise('D') | |
const endAction = () => printablePromise('Done !') | |
const flag = true | |
let promiseChain | |
if (flag) { | |
promiseChain = actionA().then(actionB) | |
} else { | |
promiseChain = actionC() | |
} | |
promiseChain | |
.then(actionD) | |
.then(endAction) | |
/* | |
Excepted print: | |
A | |
B | |
D | |
Done ! | |
Prints: | |
A | |
B | |
D | |
Done ! | |
*/ |
This file contains hidden or 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
const printablePromise = text => | |
new Promise(resolve => { | |
console.log(text) | |
return resolve() | |
}) | |
const actionA = () => printablePromise('A') | |
const actionB = () => printablePromise('B') | |
const actionC = () => printablePromise('C') | |
const actionD = () => printablePromise('D') | |
const endAction = () => printablePromise('Done !') | |
const flag = true | |
let promiseChain = actionC() | |
if (flag) { | |
promiseChain = actionA().then(actionB) | |
} | |
promiseChain | |
.then(actionD) | |
.then(endAction) | |
/* | |
Excepted print: | |
A | |
B | |
D | |
Done ! | |
Prints: | |
C | |
A | |
B | |
D | |
Done ! | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment