JavaScript promises: 3 gotchas and tricks to avoid them

Promises, handlers, and chains are hardly straightforward. Follow these tips to keep your code clean and simple

JavaScript promises: 3 gotchas and tricks to avoid them
Thinkstock

Now that we’ve covered some of the basic concepts and implementation of promises, let’s take a look at three promises gotchas that I’ve seen trip up developers as well as some tricks that might be useful.

Gotcha #1: Promise handlers return promises

If you’re returning information from a then or catch handler, it will always be wrapped in a promise, if it isn’t a promise already. You never need to write code like this:

firstAjaxCall.then(() => {
  return new Promise((resolve, reject) => {
    nextAjaxCall().then(() => resolve());
  });
});

In the case above, since nextAjaxCall also returns a promise, you can just do this:

To continue reading this article register now

How to choose a low-code development platform