Skip to content

Instantly share code, notes, and snippets.

@donabrams
Created October 21, 2011 13:35
Show Gist options
  • Select an option

  • Save donabrams/1303864 to your computer and use it in GitHub Desktop.

Select an option

Save donabrams/1303864 to your computer and use it in GitHub Desktop.
Add Timeout to Deferred
define(["jquery"],
function($) {
var addTimeout = $.addTimeoutToDeferred = function(promise, timeout) {
if (promise.isResolved() || promise.isRejected())
return promise;
var withTimeout = $.Deferred();
promise.then(function(){withTimeout.resolve(arguments);}, function(){withTimeout.reject(arguments);});
setTimeout(function() {withTimeout.reject();}, maxWait);
return withTimeout.promise();
}
return addTimeout;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment