Skip to content

Instantly share code, notes, and snippets.

@yeliu84
Last active August 29, 2015 13:56
Show Gist options
  • Save yeliu84/8978855 to your computer and use it in GitHub Desktop.
Save yeliu84/8978855 to your computer and use it in GitHub Desktop.
HTTP request with timer by promise
// http://www.slideshare.net/domenicdenicola/the-promised-land-in-angular?utm_source=ng-newsletter&utm_campaign=356fbe22fa-AngularJS_Newsletter_2_11_142_10_2014&utm_medium=email&utm_term=0_fa61364f13-356fbe22fa-90279177
function timer(promise, ms) {
var deferred = $q.defer();
promise.then(deferred.resolve, deferred.reject);
setTimeout(function() {
deferred.reject(new Error('oops, timed out!'));
}, ms);
return deferred.promise;
}
function httpGetWithTimer(url, ms) {
return timer(http.get(url), ms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment