Created
March 14, 2019 11:40
-
-
Save julien-sarazin/26fce8ceabf0d35ec49cdbeb349b9a6b to your computer and use it in GitHub Desktop.
How promises should be used.
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
return getSomethingAsync() | |
.then(processData) | |
.then(doSomethingEvenMoreComplex) | |
.then(butWithVeryExplicitNaming) | |
.catch(handlingErrorWithStyle) | |
function getSomethingAsync() { | |
return http.request() // basically initiate a promisified call | |
} | |
function processData(response) { | |
// Taking the response.body for example as a initial stream of data | |
} | |
function doSomethingEvenMoreComplex(data) { | |
// Split your responsability | |
} | |
function butWithVeryExplicitNaming(piped_data) { | |
// Keep you code clear and you function PURE | |
} | |
function handlingErrorWithStyle(error) { | |
// Gracefuly handle your errors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment