Skip to content

Instantly share code, notes, and snippets.

@FagnerMartinsBrack
Created March 27, 2016 07:42
Show Gist options
  • Save FagnerMartinsBrack/889960035bb51eb74c5a to your computer and use it in GitHub Desktop.
Save FagnerMartinsBrack/889960035bb51eb74c5a to your computer and use it in GitHub Desktop.
(Medium) Promises + Sync Code = Disaster - Correct Usage of Sync and Async
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