JavaScript Promises The Easy Way?‍??.

What is a Promise in JavaScript:

Definition by MDN:

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action’s eventual success value or fa…


This content originally appeared on DEV Community and was authored by Lokesh_Choudhary

What is a Promise in JavaScript:

Definition by MDN:

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

Source(MDN): Link Here

Thanks for reading, I hope now you now understand everything.

What, No? Didn't Understand.

No problem that why I am here writing this beautiful article for you.

The literal meaning of a promise.

Your parents promised you to buy a new computer, but on a condition that you have to get good grades in school

In our real life this is a meaning of a Promise, So it highly depends that you could get a new computer or couldn't if you didn't get good grades in school and even if you did well in exams until the results come the promise from your parent will be in a pending state.

Just like that in JavaScript Programming a Promise has three states:

  • Pending:
    You will either get new computer or not.

  • Fulfilled/Resolved:
    You will get a new computer for sure.

  • Rejected:
    Sorry, you will not get a new computer.

Now enough for the example lets get started with some coding??.

code

So what is happening is this code example is that, I have assigned a boolean value goodGrades and also in some real world cases this could be a external fetch request you are making to get some data or any other function for which you are not sure to get a result that's why you use a Promise.

If goodGrades is true you the Promise will resolve or otherwise if false the Promise will reject and give the specified Error (reason).

Also you can chain a Promise with then() and catch() or put in a try catch block.

So this is the most basic way I could found to explain JavaScript Promises.

Thanks for reading.

This is my first post here, feedbacks are appreciated.

And also about what topic should I write next?
More on JavaScript?
Backend with NodeJs/ExpressJs?
Databases?
Or Anything else.


This content originally appeared on DEV Community and was authored by Lokesh_Choudhary


Print Share Comment Cite Upload Translate Updates
APA

Lokesh_Choudhary | Sciencx (2021-08-26T05:55:03+00:00) JavaScript Promises The Easy Way?‍??.. Retrieved from https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/

MLA
" » JavaScript Promises The Easy Way?‍??.." Lokesh_Choudhary | Sciencx - Thursday August 26, 2021, https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/
HARVARD
Lokesh_Choudhary | Sciencx Thursday August 26, 2021 » JavaScript Promises The Easy Way?‍??.., viewed ,<https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/>
VANCOUVER
Lokesh_Choudhary | Sciencx - » JavaScript Promises The Easy Way?‍??.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/
CHICAGO
" » JavaScript Promises The Easy Way?‍??.." Lokesh_Choudhary | Sciencx - Accessed . https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/
IEEE
" » JavaScript Promises The Easy Way?‍??.." Lokesh_Choudhary | Sciencx [Online]. Available: https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/. [Accessed: ]
rf:citation
» JavaScript Promises The Easy Way?‍??. | Lokesh_Choudhary | Sciencx | https://www.scien.cx/2021/08/26/javascript-promises-the-easy-way%f0%9f%91%a8%e2%80%8d%f0%9f%8e%93%f0%9f%a4%93/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.