JSFiddle - React, Tailwind, and code Playground
JavaScript
function getRandomNumber(min, max) {
// generate the number and see if it's even
var number = Math.floor(Math.random() * max) + min;
var isEven = number % 2 == 0;
// define the final handler
var finalHandler;
// call this handler async to allow the caller to map the actual methods
setTimeout(function() {
if (finalHandler) finalHandler(number);
}, 0)
// return the object that exposes our two methods
return {
thatsOdd: function(h) {
// map the odd handler that will be called async
if (!isEven) finalHandler = h;
// let each method return this, to allow chaining
return this;
},
thatsEven: function(h) {
// map the even handler that will be called async
if (isEven) finalHandler = h;
// let each method return this, to allow chaining
return this;
}
};
}
getRandomNumber(1, 10)
.thatsOdd(function(number){
alert(number + ' is odd!')
})
.thatsEven(function(number){
alert(number + ' is even!')
})