Last active
March 27, 2016 07:34
-
-
Save FagnerMartinsBrack/52791bfd26a3308dd428 to your computer and use it in GitHub Desktop.
(Medium) Promises + Sync Code = Disaster - Promises With Sync Code
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
const fetchSoccerResults = fetchSoccerResultsFromResource( ... ); | |
const loadSoccerMatchesToVariable = function( _soccerMatches ) { | |
soccerMatches = _soccerMatches; | |
}; | |
const loadSoccerResultsToVariable = function( _soccerResults ) { | |
soccerResults = _soccerResults; | |
} | |
const mapDetailedMatches = function(){ | |
return soccerMatches.map(function( match ) { | |
return { | |
name: match.name, | |
score: findScoreForMatch( match, soccerResults ) | |
}; | |
}); | |
}; | |
const printDetailedMatchesToScreen = function( detailedMatches ) { | |
detailedMatches.forEach(function( detailedMatch ) { | |
const name = detailedMatch.name; | |
const score = detailedMatch.score; | |
console.log( `${name} with the score ${score}` ); | |
}); | |
}; | |
const printError = function( err ) { | |
console.error( 'Failed to print the score', err ); | |
} | |
let soccerMatches; | |
let soccerResults; | |
fetchSoccerMatchesFromResource( ... ); | |
.then( loadSoccerMatchesToVariable ) | |
.then( fetchSoccerResults ) | |
.then( loadSoccerResultsToVariable) | |
.then( mapDetailedMatches ) | |
.then( printDetailedMatchesToScreen ); | |
}) | |
.catch( printError ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment