Skip to content

Instantly share code, notes, and snippets.

@julien-sarazin
Created March 14, 2019 11:40
Show Gist options
  • Save julien-sarazin/26fce8ceabf0d35ec49cdbeb349b9a6b to your computer and use it in GitHub Desktop.
Save julien-sarazin/26fce8ceabf0d35ec49cdbeb349b9a6b to your computer and use it in GitHub Desktop.
How promises should be used.
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