Last active
January 6, 2017 17:14
-
-
Save appellation/89eaaac33d092ef321506d7b15d4af54 to your computer and use it in GitHub Desktop.
A quick promise chaining demonstration.
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
// not chaining | |
asyncFunction().then(data => { | |
asyncFunction(data).then(data2 => { | |
asyncFunction(data2); | |
}).catch(console.error); | |
}); | |
// chaining | |
asyncFunction.then(data => { | |
return asyncFunction(data); | |
}).catch(console.error).then(data2 => { // this catch statement would catch all previously chained promises | |
return asyncFunction(data2); | |
}); | |
function asyncFunction(say) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(say || 'async complete'); | |
}, 1000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment