promise.js
1 KB
var $ = require('yoho-jquery');
$.extend({
defer: function(func) {
return function() {
var defer = $.Deferred(); // eslint-disable-line
var args = Array.prototype.slice.call(arguments);
args.push(defer.resolve, defer.reject);
func && func.apply(undefined, args); // eslint-disable-line
return defer.promise();
};
},
reject: function() {
var defer = $.Deferred(); // eslint-disable-line
var args = Array.prototype.slice.call(arguments);
return defer.reject.apply(defer, args).promise();
},
resolve: function() {
var defer = $.Deferred(); // eslint-disable-line
var args = Array.prototype.slice.call(arguments);
return defer.resolve.apply(defer, args).promise();
},
sleep: function(ms) {
var defer = $.Deferred(); // eslint-disable-line
window.setTimeout(function() {
defer.resolve();
}, ms);
return defer.promise();
}
});