Created
August 20, 2019 20:25
-
-
Save aashish-chaubey/17542f5a74bcd17dd927fe1b0b987f50 to your computer and use it in GitHub Desktop.
Implementing and using the basic promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Aashish Chaubey | |
* @link https://github.com/aashish-chaubey | |
*/ | |
/** | |
* First Create a Promise my implementing it with a `new` keyword | |
* Do all the heavy weight operations in the promise | |
* Store the promise in a variable | |
*/ | |
let myPromise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
let rand = Math.random() | |
console.log(rand) | |
if(rand < 0.5) { | |
resolve("The Random Value is less than 0.5") | |
} else { | |
reject("Random Value is greater then 0.5") | |
} | |
}, 2000) | |
}) | |
/** | |
* Execute the promise | |
* Call the `.then()` and `.catch()` for the resolve and rejected values respectively | |
*/ | |
myPromise | |
.then(resp => { | |
console.log(resp); | |
}) | |
.catch(err => { | |
console.log(err); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important to note is that: