FeaturedIT topics

JavaScript promises: 3 gotchas and tricks to avoid them

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:

Related Articles

Back to top button