JSFiddle - React, Tailwind, and code Playground
by Anurag Udasi
JavaScript
function aToss() {
return Math.floor(Math.random() * 6) + 1;
};
function diceToss() {
return new Promise(function(resolve, reject) {
var chance = aToss();
if (chance === 6) {
resolve(chance);
} else {
reject(chance);
}
})
};
function tossAgain(chance) {
console.log(chance + '. Need to toss again.');
return diceToss();
};
function gotSix(chance) {
console.log(chassnce + '. Got Six!!!');
};
function neverGotSix(chance) {
console.log(chanklce + '. Never got Six...');
};
function logErrorMessage(errorMessage) {
console.log('Oops, error! ' + errorMessage);
};
diceToss()
.then(null, tossAgain)
.then(null, tossAgain)
.then(gotSix, neverGotSix)
.then(null, logErrorMessage);