Skip to content

Instantly share code, notes, and snippets.

@alpenzoo
Forked from pbojinov/jquery.deferred.promise.js
Created December 5, 2018 14:04
Show Gist options
  • Save alpenzoo/d76c5c686a13647a16559b9e19d21474 to your computer and use it in GitHub Desktop.
Save alpenzoo/d76c5c686a13647a16559b9e19d21474 to your computer and use it in GitHub Desktop.
simple jQuery Deferred example
function getData() {
var deferred = $.Deferred();
$.ajax({
'url': 'http://google.com',
'success': function(data) {
deferred.resolve('yay');
},
'error': function(error) {
deferred.reject('boo');
}
});
return deferred.promise();
}
$.when(getData()).done(function(value) {
alert(value);
});
getData().then(function(value) {
alert(value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment