Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Last active August 31, 2022 02:38
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save pbojinov/ca377fd51c69531a8029 to your computer and use it in GitHub Desktop.
Save pbojinov/ca377fd51c69531a8029 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);
});
Copy link

ghost commented Dec 24, 2019

I think its awesome you doing gists of your work! Keep it up man! That helps others!!!

@ydarwin
Copy link

ydarwin commented Aug 30, 2022

Complete 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);
},
function (error) {
    alert(error);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment