JSFiddle - React, Tailwind, and code Playground
by James Allardice
HTML
<script src="https://rawgithub.com/kriskowal/q/master/q.min.js"></script>
JavaScript
function Lib() {}
Lib.prototype.check = function () {
this.queue = [];
return this;
};
Lib.prototype.then = function (fulfilled, rejected) {
return Q.all(this.queue).then(fulfilled, rejected);
};
var methods = {
method1: function (deferred) {
setTimeout(function () {
deferred.resolve("timer done");
}, 500);
return deferred.promise;
},
method2: function (deferred) {
setTimeout(function () {
deferred.reject(new Error("reject"));
//deferred.resolve("timer 2 done");
}, 500);
return deferred.promise;
}
};
Object.keys(methods).forEach(function (method) {
Lib.prototype[method] = function () {
var args = [].slice.call(arguments);
args.unshift(Q.defer());
this.queue.push(methods[method].apply(this, args));
return this;
};
});
var l = new Lib();
l.check().method1().method2().then(function (v) {
console.log("fulfilled", v);
}, function (v) {
console.log("rejected", v);
});