For the love of Promises, stop nesting your chains.then()

I’ve been spending a lot of time in some express JS applications lately. I love express, but generally only when I can work with ES7 syntax.

However, lately I’ve been tackling deep, nested, unreadable promise chains.

app.use((req, res) => {
  runSomeDatabaseCode().then((response) => {
    const newResponse = response.manipulate();
    runSomeAPICode(newResponse).then((data) => {
      if (!data) throw new Error("It Broke");
      res.status(200).send(data)
    }).catch(err => res(500).send('Error'))
  }).catch(err => res(500).send('Error'))
});

For the love of all things asynchronous, please tidy this up. The solution in this instance is quite simple — linear promise chains.

app.use((req, res) => {
  new Promise(r => r()) // Just an empty promise to start the structure
  .then(() => new Promise((resolve, reject) => {
    runSomeDatabaseCode()
    .then(response => {
      const newResponse = response.manipulate();
      resolve(newResponse) // Pass the response to the next in the chain
    })
    .catch(error => {
      reject(error)
    })
  }))
  .then((newResponse) => new Promise((resolve, reject) => {
    runSomeAPICode(newResponse)
    .then(data => {
      if (!data) throw new Error('It Broke')
      resolve(data)
    })
  }))
  .then(data => {
    res.status(200).send(data); // resolve to express
  })
  .catch(err => {
    res.status(500).send(err); // resolve errors to express
  })
})

These linear structures are much better as they free up the scope for your functions, remove unnecessary repetition in code and generally allow a better reading experience with less left-right scrolling.

For other cool Promise-related stuff, check out Bluebird. Developers will thank you for it when they are reading your beautifully structured code.

 
2
Kudos
 
2
Kudos

Now read this

Another post by somebody who left a popular blogging platform

Gosh. Shock horror. I left Medium.com. I guess, it was an easy decision after they stopped their custom domain approval process for a “short review”. Psst! Still waiting, guys! I’ve been eager to find something better, so I’ve decided to... Continue →