jQuery promise and AJAX
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
by ken si
HTML
<div id="output"></div>
JavaScript
// Info: http://net.tutsplus.com/tutorials/javascript-ajax/wrangle-async-tasks-with-jquery-promises/
function logger(txt) {
$('#output').append(txt + "<br>");
}
var url = '/echo/js/';
var data = {js: 'hello'};
var promise = $.getJSON(url, data, function(json) {
//alert(JSON.stringify(json));
logger('ajax1 done');
});
promise.done(function(data) {
logger('ajax1 ok: ' + JSON.stringify(data));
});
promise.fail(function(data) {
logger('ajax2 fail: ' + JSON.stringify(data));
});
logger('script done');