DEV Community

Cover image for What A Promise ! '1st'
AbdELrahman Mohamed
AbdELrahman Mohamed

Posted on

What A Promise ! '1st'

Hoo-AH , In this CODE CAPSULE we will go throw Promises in js and learn how to use it , all codes will be in one Js file with examples and results, So,enough videos just take 5 mins to read it.


What is Promise in JS ?

A Promise is a very useful Object helper to work with Asynchronous Tasks and It's protector from the callback hell !


let's say we have code that do some magic and that magic is depend on server response , in this case Promises give us the advantage of complete our code execution without waiting for the server response , and make a part of our code execite when the result fullfilld ot failed

Image description

Here we create a promise with Promise Constructor function

Image description

Adding function as Executor as an argumnent for
Promise Constructor function and this executor takes two arguments Resolve in case of success and reject in case of fail .

Image description

we create a time interval to simulate as a server response after a while, then we will resolve the value .

Image description

Now , we will log the value after 1500ms or '1.5' seconds to the console and it will be "Done"

Image description

See u in another CodeCapsule !

Top comments (0)