DEV Community

Cover image for All about Promises in JavaScript

All about Promises in JavaScript

Code of Relevancy on February 23, 2023

In JavaScript, a Promise is an object that represents a value that may not be available yet, but will be resolved in the future. Promises are used ...
Collapse
 
ant_f_dev profile image
Anthony Fung •

Great article. In addition to this, you can also run multiple promises and only continue once they are all complete using Promise.all. This is great way to, e.g. fetch data using two different requests, and then combine them once both requests complete.

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you for reading my article and your valuable feedback. As per your suggestion, I have updated this article by adding a new section "Multiple Promises in Parallel".

Collapse
 
ant_f_dev profile image
Anthony Fung •

A great addition - I've only ever had to use all and occasionally any, so didn't know about race and allSettled until reading it - thanks!

Thread Thread
 
codeofrelevancy profile image
Code of Relevancy •

You're welcome..

Collapse
 
mcwolfmm profile image
mcwolfmm •

Ok, why are you giving a code with pictures?

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

For better representation of my articles. Moving forward, I will add source code as well..
Thank you for valuable feedback. I appreciate it..

Collapse
 
cwilby profile image
Cameron Wilby •

Excellent post! A lot of this translates into helpful insight when writing async/await functions, which are themselves based on promises!

developer.mozilla.org/en-US/docs/W...

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thanks for reading and feedback..

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Very excited to announce that we have reached 1K followers in our community. We couldn't have done it without your support and engagement. We want to thank each and every one of you for being a part of our adventure..

We look forward to continuing to grow together & providing even more value to our community.

Happy coding!!!

Collapse
 
stakedesigner profile image
StakeDesigner •

wow, the great present article

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you..

Collapse
 
adii9 profile image
Aditya Mathur •

This is a great blog. I really loved the way promises has been explained with in-depth knowledge. More over the solution of the probelms mentioned made it even better. Keep up the good work.

Collapse
 
codeofrelevancy profile image
Code of Relevancy • • Edited

Thank you Aditya for your valuable feedback..

Collapse
 
dealwith profile image
Hleb Kryshyn •

Thanks for the article! What program are you using for generating such cool code snippets and GIF animations?

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you for your valuable feedback.

I use these tools:
Miro - For diagrams
Canva - For GIF animations
Ray.so Image of code snippets

Collapse
 
dealwith profile image
Hleb Kryshyn •

thanks

Thread Thread
 
codeofrelevancy profile image
Code of Relevancy •

You're welcome..

Collapse
 
thebrown profile image
Saleumsack •

Great aticle

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

I’m glad you found it helpful. Thank you boss for your vaunted feedback

Collapse
 
shallfer7 profile image
Charles Fernandes •

Great article, great examples

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you 🙏 for your valuable feedback..

Collapse
 
ypdev19 profile image
Yuli Petrilli •

Great article! Saving it to study later because i still struggle when working with promises. Thank u!

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you for your feedback. Glad to hear that you found it useful. Best of luck in your programming journey..

Collapse
 
chiranjib_b profile image
Chiranjib •

Fantastic explanation. May I use the link to your article in mine?

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you for your feedback. Yes you can.. 🙏

Collapse
 
thecheapaudiophile profile image
Griff Polk •

Joining on 12/22/22 be like OFF BY ONLY ONE NUMBER NOOOOOOOOO

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Hahaha, but we're both here now and that's what matters..

Collapse
 
dansagam profile image
dansagam •

This article is great, though I have never have reason to use Promise.setttled but I find it very good

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

I’m glad you found it helpful. Thank you 🙏 for reading

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • • Edited

This is not all about promises, there are much more, I suggest a book by Nicholas C. Zakas

Collapse
 
huylong profile image
huylong •

Thank you for share.

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

You're welcome. I am glad you found it useful read..

Collapse
 
clericcoder profile image
Abdulsalaam Noibi •

Wow, Thanks for writing this Great Article. I really learn a lot

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Glad you found it helpful. Thanking you for reading and your valuable feedback..

Collapse
 
thecheapaudiophile profile image
Griff Polk •

Wow! This is really cool, i feel i can use this really well!

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Go for it. Thank you @grey41 for your feedback..

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you boss. I appreciate your valuable feedback..

Collapse
 
sammaji profile image
Samyabrata Maji •

Great blog

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you bro for your valuable feedback.

Collapse
 
gautamvaishnav profile image
Gautam Vaishnav •

was a great article, made my day

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you for your feedback

Collapse
 
amirfcf72 profile image
Amir •

Thanks great article

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you Amir, for reading and feedback..

Collapse
 
anthonycroft profile image
Anthony Croft • • Edited

It maybe just my interpretation of your words, but I could not see the difference between Promise.any() and Promise.race() from the above explanations.

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Hello @anthonycroft
Sorry for the confusions. I will take care of it moving forward.

Let me clear it for you..

Promise.race is settled when any of the promises is settled even if they are fulfilled or rejected.
On the otherside,
Promise.any is settled when any of the promises is fulfilled or if all of them are rejected, it will be rejected with an AggregateError.

Collapse
 
shapevorks profile image
Shapevorks •

Thanks

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

You're welcome

Collapse
 
codeofrelevancy profile image
Code of Relevancy •

Thank you so much bro