Promise? (JavaScript)

David Lago
2 min readSep 13, 2021

--

What is a promise? Well, it’s an object. Just like everything in JavaScript mostly. Promises can have three states first one being pending, the second a fulfilled promise and finally a rejected promise. What you can do with promises is that you can have a fetch request to a URL and have it have a callback to one of those three states above (pending, fulfilled & rejected). What also could be done is having the promise be executed with a then() or catch(). With then() the promise must be fulfilled with catch() being the opposite, the promise is rejected.

Graph from Mozilla showing off the possibilities

The beauty of of a promise is JavaScript is unlike a callback, with a promise you can add a .then() to make your code cleaner and be able to run whatever URL or any function you desire much easier. It’s way better than “callback hell” which contains so many indentations of code piling on top of one another without using then().

an example of “callback Hell”
an example of using a promise

This was extremely short but I wanted to share my knowledge of promises and will soon be creating an updated version in the near future! Thank you so much for reading and have an amazing day!

--

--

Responses (1)