JSFiddle - React, Tailwind, and code Playground
by ryanwheale
JavaScript
function MyClass () {
function checkAge() {
console.log("checkAge");
var def = $.Deferred();
setTimeout(function() {
def.resolve(Math.random() < 0.3 ? 17 : 19);
}, 200);
return def.then(function( age ) {
console.log("checkAgeDone:", age);
if ( age > 18 ) {
return checkProximity();
}
return "You must be at least 18";;
});
}
function checkProximity() {
console.log("checkProximity");
var def = $.Deferred();
setTimeout(function() {
def.resolve(Math.random() < 0.5 ? 99 : 101);
}, 200);
return def.then(function( distance ) {
console.log("proximityCheckDone", distance);
if (distance < 100) {
return checkCoolness();
}
return "You must be located within 100 units.";
});
}
function checkCoolness() {
console.log("checkCoolness");
var def = $.Deferred();
setTimeout(function() {
def.resolve(Math.random() < 0.6 ? 420 : 2);
}, 200);
return def.then(function( coolScore ) {
return coolScore === 420 ? true : "You're just not cool enough";
});
}
return {
checkQualification: checkAge
};
}
var instance = new MyClass();
instance.checkQualification().then(function (isQualified) {
if (isQualified === true) {
console.log("WOO HOO OLD NEARBY COOL GUY!");
} else {
console.log("NOT QUALIFIED:", isQualified);
}
});