Created
March 27, 2016 07:42
-
-
Save FagnerMartinsBrack/889960035bb51eb74c5a to your computer and use it in GitHub Desktop.
(Medium) Promises + Sync Code = Disaster - Correct Usage of Sync and Async
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 mapDetailedMatches = function( soccerMatches, soccerResults ){ | |
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}` ); | |
}); | |
}; | |
let soccerMatches; | |
fetchSoccerMatchesFromResource( ... ) | |
.then(function( _soccerMatches ) { | |
soccerMatches = _soccerMatches; | |
return fetchSoccerResultsFromResource( ... ); | |
}) | |
.then(function( soccerResults ) { | |
const detailedMatches = mapDetailedMatches( | |
soccerMatches, | |
soccerResults | |
); | |
printDetailedMatchesToScreen( detailedMatches ); | |
}) | |
.catch(function( err ) { | |
console.error( 'Failed to print the score', err ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment