Quick Tip: Promise Chains


It's pretty common to see promise chains in your Javascript code.

There are a few things to keep in mind that will make your code more readable and more bulletproof.

A Bad Example

getNHLTeams()
    .then(function(teams) {
        // long function body here

        getScores()
            .then(function() {
                //
            })
            .then(function() {
                //
            });
    });

A Good Example

getNHLTeams()
    .then(sortTeams)
    .then(pickFirstTeam)
    .then(getScores) // another promise
    .then(calculateWinStreak)
    .catch(handleError);

A couple best practises are:

• Use named functions instead of anonymous ones
• Avoid nesting multiple promises
• Add a catch in case something fails

You can see in the above example that nesting is avoided completely. This logic also contains no business logic but instead focuses on coordinating the order of which things need to run.

Thoughts? Hit me up on twitter @brenelz

Notice spelling mistakes or outdated content? Make a pull request!