What Happens if You Don’t Resolve or Reject A Promise?
Photo by https://unsplash.com/@cgower

What Happens if You Don’t Resolve or Reject A Promise?

JavaScript developers often use promises to handle asynchronous operations. A?Promise?object can be in a?pending,?fulfilled, or?rejected?state, depending on whether it is resolved or rejected. But what happens if a?Promise?is not resolved or rejected? Let’s find out.

To get to the bottom of this question, let’s remember the basics of how promises work.

How does Promise Works?

The definition of?Promise?according to the?documentation?sounds like this:

The?Promise?object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

This means that a?Promise, after execution, tells us whether it succeeded or failed. And it returns the value it resolved with or the error it was rejected with to appropriate?.then()?and?.catch()?methods callbacks.

Example #1.?Promise, which is successfully resolved:

Альтернативный текст для этого изображения не предоставлен

After performing the asynchronous operation?setTimeout()?, I call the?resolve()?method. The?Promise.resolve()?method returns a?Promise?object that is resolved with a given value. Once a?Promise?is?fulfilled?the respective?.then()?callback will be called.

After executing this code, I got the following output on the console:

Альтернативный текст для этого изображения не предоставлен

The callback I passed to?.then()?method will only be executed when the?Promise?is completed by calling the?resolve()?method.

What happens if I call?reject()?method instead of the?resolve()?

Example #2.?Promise, which is rejected:

Альтернативный текст для этого изображения не предоставлен

In this example, after executing?setTimeout(), I call the?reject()?method. The?Promise.reject()?method returns a?Promise?object that is rejected with a given reason. Once a?Promise?is?rejected?the respective?.catch()?callback will be called.

I got the following output on the console after running this code:

Альтернативный текст для этого изображения не предоставлен

The callback I passed to?.catch()?method will only be executed when the?Promise?is completed by calling the?reject()?method.

What if a promise wasn’t resolved or rejected?

Example #3.?Promise, which wasn’t resolved or rejected:

Альтернативный текст для этого изображения не предоставлен

In this example, after running?setTimeout(), I don’t call methods that would change the state of the?Promise.

I got the following output on the console after running this code:

Альтернативный текст для этого изображения не предоставлен

It turns out that the callbacks I passed in the?.then()?and?.catch()?methods will never be executed.

Why does this happen?

A?Promise?is just a JavaScript object with properties. So, unless I use the?resolve()?or?reject()?methods, promise's status will never change from?pending?to something else.

And there is no problem with code performance. A?Promise?will be collected by the garbage collector even if its status is still?pending?if it is not referenced in the code.

The most important thing, in this case, is that any code in?.then()?or?.catch()?listeners will never be called. If you use a?Promise, then your code expects the?Promise?to be resolved or rejected. Otherwise, your code will simply never execute.

Conclusions

The answer to the question turned out to be quite simple. If we do not change the status of the?Promise, it's?.then()?and?.catch()?methods will simply never be called. This is how the?Promise?was designed and this is its normal behavior.

Now you don’t have to be afraid to hear this question at a job interview.

What are some interesting questions you’ve heard about promises? I’d be interested in reading them in the comments.

Thank you for your time. See you in the next ones ??.

More articles by Evgeny Kirichuk on Medium:

要查看或添加评论,请登录

Evgeny Kirichuk的更多文章

社区洞察

其他会员也浏览了