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
Here we create a promise with Promise Constructor function
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 .
we create a time interval to simulate as a server response after a while, then we will resolve the value .
Now , we will log the value after 1500ms or '1.5' seconds to the console and it will be "Done"
Top comments (0)