Skip to content

Instantly share code, notes, and snippets.

@FagnerMartinsBrack
Last active March 27, 2016 07:34
Show Gist options
  • Save FagnerMartinsBrack/52791bfd26a3308dd428 to your computer and use it in GitHub Desktop.
Save FagnerMartinsBrack/52791bfd26a3308dd428 to your computer and use it in GitHub Desktop.
(Medium) Promises + Sync Code = Disaster - Promises With Sync Code
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