Last active
March 28, 2021 22:40
-
-
Save dd701116/f20cf5e77fbfecda395501f635474373 to your computer and use it in GitHub Desktop.
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
// The method wich return a promise | |
function thePromiseGiver() { | |
return new Promise((resolve, reject) => { | |
let dice = Math.floor(Math.random() * Math.floor(6)); | |
if (dice>=3) { | |
resolve(dice); | |
}else{ | |
reject(new Error(dice)); | |
} | |
}); | |
} | |
// What to do if everythings right | |
function yes () { | |
console.log("Yes, I'll do it !"); | |
} | |
// What to do if we have a bad thing | |
function no () { | |
console.error("Nope, drop it !"); | |
} | |
// Execute the first method and THEN call the next one according to the result | |
thePromiseGiver().then(yes,no); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment