jQuery Simple deferred object...
- http://api.jquery.com/deferred.done/
by b_long
HTML
<body class="put-stuff-here">
<h4>Things will be appended here</h4>
</body>
JavaScript
var logIt = function(msg){
$(".put-stuff-here").append(msg + "<br/>");
};
var doSomeAsynchronousStuff = function () {
var dfd = new $.Deferred();
var myData = {
json: JSON.stringify({
text: "Echoing JSON is silly",
foo: "foo",
bar: "bar"
})
};
var jqXHR = $.ajax({
type: 'POST',
data: myData,
dataType: 'json',
url: '/echo/json/',
success: function (data) {
console.log(JSON.stringify(data));
dfd.resolve(data);
}
})
.done(function () {
logIt("success");
})
.fail(function () {
logIt("error");
})
.always(function () {
logIt("complete");
});
return dfd.promise();
};
$.when(doSomeAsynchronousStuff()).done(function (d2) {
logIt('Server returned this JSON: <pre>' + JSON.stringify(d2) + '</pre>');
});