How Promises Actually Work in JavaScript - by Dev by RayRay - Better Programming
How Promises Actually Work in JavaScript - by Dev by RayRay - Better Programming
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Dev by RayRay
Mar 11 · 4 min read
In the current age of JavaScript, Promises are the default way to handle asynchronous
behavior in JavaScript. But how do they work? Why should you understand them very
well?
In this article, I will dive into JavaScript Promises to understand what they do and
when you should use them.
To make Medium work, we log user data. By using Medium, you agree to our Privacy Policy, including
cookie policy.
But I don’t tell you when that promise will be fulfilled, so life goes on…
Fulfillment
One day, I fulfill that promise. It makes you so happy that you post about it on Twitter!
Rejection
One day, I tell you that I can’t fulfill the promise.
You make a sad post on Twitter about how I didn’t do what I had promised.
Both scenarios cause an action. The first is a positive one, and the next is a negative
one.
Keep this scenario in mind while going through how JavaScript Promises work.
But when you want to get data from an API, you don’t know how fast you will get the
data back. Rather, you don’t know if you will get the data or an error yet. Errors
happen all the time, and those things can’t be planned. But we can be prepared for it.
So when you’re waiting to get a result from the API, your code is blocking the browser.
It will freeze the browser. Neither we nor our users are happy about that at all!
returns a Promise . Then, we will dive into how you can use a function that returns a
Promise .
1 function doSomething(value) {
2 return new Promise((resolve, reject) => {
3 // Fake a API call
4 setTimeout(() => {
5 if(value) {
6 resolve(value)
7 } else {
8 reject('The Value Was Not Truthy')
9 }
10 }, 5000)
11 });
12 }
dosomething.js
hosted with ❤ by GitHub view raw
States
According to MDN Web Docs, a JavaScript Promise can have one of three states:
Pending
The pending state is the initial state. This means that we have this state as soon we call
the doSomething() function, so we don't know yet if the Promise is rejected or resolved.
Resolved
Inmake
To the example, if the
Medium work, value
we log is truthy,
user data. the
By using Promise
Medium, will be
you agree resolved.
to our In thisincluding
Privacy Policy, case, we
cookie policy.
pass the variable value in it to use it when we would call this function.
Rejected
In the example, if the value is falsy, the Promise will be rejected. In this case, we pass
an error message. It's just a string here, but when you make an Ajax request, you pass
the server's error.
You can recognize a function that returns a Promise by the .then() method or an
await keyword. The catch will be called if there is an error in your Promise . So
Promises are used in a lot of JavaScript libraries and frameworks as well. But the
simplest web API is the Fetch API, which you should use for making Ajax requests.
Check the tutorial below if you want to see how that works:
Conclusion
Hopefully,
To make Medium thiswork,
helpswe you to understand
log user data. By using how you agreework
Promises
Medium, to ourand what
Privacy youincluding
Policy, can do with
cookie policy.
them.
Happy coding!
A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials,
advice, career opportunities, and more! Take a look.