JSFiddle - React, Tailwind, and code Playground
by bruth
JavaScript
(function() {
var _slice = [].slice;
// Gets or creates a deferred object for `name` and adds it to
// the `deferreds` object.
function _deferred(deferreds, name) {
var deferred;
if (!(deferred = deferreds[name])) {
deferred = deferreds[name] = $.Deferred();
}
return deferred;
}
// Proxies promise-based methods to promiser. Also takes an object
// of name/handler pairs. The latter invocation does not return a
// promise while the former does.
function _proxy(deferreds, method, name) {
if (typeof name == 'object') {
for (var key in name) {
_proxy(deferreds, method, key, name[key]);
}
} else {
var _args = _slice(arguments, 3),
_ref = _deferred(deferreds, name);
return _ref[method].apply(_ref, _args).promise();
}
}
var _prototype = {
done: function() {
var args = [this._deferreds, 'done'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
then: function() {
var args = [this._deferreds, 'then'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
fail: function() {
var args = [this._deferreds, 'fail'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
always: function() {
var args = [this._deferreds, 'always'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
pipe: function() {
var args = [this._deferreds, 'pipe'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
progress: function() {
var args = [this._deferreds, 'progress'].concat(_slice(arguments));
return _proxy.apply(null, args);
},
resolve: function(name) {
var args = _slice(arguments, 1);
...