Skip to content

Instantly share code, notes, and snippets.

@dd701116
Last active March 28, 2021 22:40
Show Gist options
  • Save dd701116/f20cf5e77fbfecda395501f635474373 to your computer and use it in GitHub Desktop.
Save dd701116/f20cf5e77fbfecda395501f635474373 to your computer and use it in GitHub Desktop.
// 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