9 Promising Promise Tips

Kushan Joshi
5 min readFeb 21, 2018

--

Promises are great to work with! Or so does your fellow developer at work says.

This article would give you to the point no bullshit tips on how to improve your relationship with the Promises.

1. You can return a Promise inside a .then

Let me make the most important tip standout

Yes! you can return a Promise inside a .then

Also, the returned promise is automatically unwrapped in the next .then

2. You create a new Promise everytime you do .then

If you are familiar with the dot chaining style of javascript you would feel at home. But for a newcomer this might now be obvious.

In promises whenever you .then or .catch you are creating a new Promise. This promise is a composition of the promise you just chained and the .then / .catch you just attached.

Let us look at an example:

The relationship of above promises can be described neatly in a flow chart:

The important thing to note here is that promA, promB and promC are all different promises but related.

I like to think of .thening as a big massive plumbing where water will stop flowing to the children when the parent node malfunctions. For eg. if promB fails, no other node will be affected but if statusProm fails all the nodes will be affected i.e. rejected.

3. A Promise is resolved/rejected for EVERYONE

I find this as one of the most important things that makes promises great to work with. To put it in other words, if a promise is shared between multiple parts of your app, all of them would get notified when it gets resolves/reject. This also means nobody can ever mutate your promise, so please feel free to pass it around without worrying.

In the trivial example above you can that promise by design makes it difficult for anyone to do nefarious things. As I said above, Keep calm and pass promise around

4. Promise Constructor is not the solution

I have seen fellow developers exploiting the constructor style everywhere, thinking they are doing it the promise way. But this is a big lie, the actual reason is that the constructor API is very similar to the good old callback API and old habits die hard.

If you find yourself writing Promise constructors everywhere, you are doing it wrong!

To actually take a step forward and move away from callback you need to carefully minimize the amount of Promise constructor’s you use.

Let us jump to the actual use case for promise constructor:

Promise constructor should only be used when you want to convert a callback to promise. Once you have grasped this beautiful way of creating promises, it can become really tempting to use it at other places which are already promisified!

Let us look at a redundant promise constructor

☠️Wrong

💖Correct

Wrapping a promise with Promise constructor is just redundant and defeats the purpose of the promise itself.

😎Protip

If you are a nodejs person, I recommend checking out util.promisify. This tiny thing helps you convert your node style callback into promises.

5. Use Promise.resolve

Javascript provides Promise.resolve, which is a short hard for writting something like this:

This has multiple use cases and my favourite being able to convert a regular sync-javascript object into a promise.

You can also use it as a safety wrapper around a value which you are not sure if it is a promise or regular value.

6. Use Promise.reject

Javascript also provides Promise.reject, which is a short hand for this

One of my favourite use case is rejecting early with Promise.reject.

In simple words, use Promise.reject wherever you want to reject promise.

In the example below I use it in a .then

Note: You can put any value inside Promise.reject just like Promise.resolve. The reason you often find Error in a rejected promise is that it is primarily used for throwing an async error.

7. Use Promise.all

Javascript provides Promise.all, which is a shorthand for …. well I can’t come up with this 😁.

Putting it in simple words Promise.all

The following example shows when all the promises resolve:

This one shows when one of them fails:

Note: Promise.all is smart! In case of a rejection, it doesn't wait for all of the promises to complete!. Whenever any promise rejects, it immediately aborts without waiting for other promises to complete.

8. Do not fear the rejection OR

How often do we fear errors being gobbled up somewhere in between?

To overcome this fear, I give you a very simple tip:

Make the rejection handling the problem of the parent function.

Ideally, rejection handling should be at the root of your app and let all the promise rejections trickle down to it.

Do not fear writing something like this

Now if you do want to handle the rejection in your function, decide whether you want to resolve things or continue the rejection.

💘 Resolving a rejection

Resolving rejection is simple, in the .catch whatever you return would be assumed to be resolved. However there is a catch (pun intended), if you return a Promise.reject in a .catch the promise will be rejected.

💔Rejecting a Rejection

To reject a rejection is simple, don’t do anything. As I said above, let it be some other functions problem. More often than not, parent functions have a better way to handle the rejection than your current function.

The important thing to remember is, once you write a catch it means you are handling the error. This is similar to how sync try/catch works.

If you do want to intercept a rejection (I highly recommend not!)

The fine line between .then(x,y) and then(x).catch(x) The .then accepts a second callback parameter which can also be used to handle errors. This might look similar to doing something like then(x).catch(x), but both these error handlers differ in which error they catch.

I will let the following example speak for itself.

The .then(x,y) comes really handy when you want to handle an error coming from the promise you are .thening and not want to handle from the .then you just appended to the promise chain.

Note: 99.9% of the times you are better off using the simpler then(x).catch(x) .

9. Avoid the .then hell

This tip is pretty simple, try to avoid the .then inside a .then or .catch. Trust me it can be avoided more often than you think.

☠️Wrong

💖Correct

Sometimes it does happen that we need multiple variables in a .then scope and there is no option but to create another .then chain.

I recommend using the ES6 destructuring power mixed with Promise.all to the rescue!

Note: You can also use async/await to solve this problem if your node/browser/boss/conscious allows!

I really hope this article helped you in understand Promises.

Please check out my previous blog posts.

If you ❤️ this article, please share this article to spread the words.

Reach out to me on Twitter @kushan2020.

Originally published at gist.github.com.

--

--