Skip to content

Instantly share code, notes, and snippets.

@danielbdias
Last active March 6, 2017 19:57
Show Gist options
  • Save danielbdias/495829c9909aa9509673364a8824f9ce to your computer and use it in GitHub Desktop.
Save danielbdias/495829c9909aa9509673364a8824f9ce to your computer and use it in GitHub Desktop.
To set promiseChain or not to set promiseChain, that is the question
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 !
*/
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