Brenelz Web Solutions

Brenley Dueck


Quick Tip: Promise Chains

November 19, 2016

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

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

            .then(function() {
            .then(function() {

A Good Example

    .then(getScores) // another promise

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