HTML and CSS Reference
In-Depth Information
url : "/server/tasks.json",
});
$.when(promise1, promise2).done(function(data1, data2) {
console.log(data1[0]);
console.log(data2[0]);
console.log("Both requests have completed");
});
promise.fail(function() {
console.log('A failure occurred');
});
The great news about promises is that you can use them yourself in your own code APIs.
Let's consider a function that performs caching scenario described above:
cachedTasks = function() {
var tasks = null;
return {
getTasks : function() {
var deferred = $.Deferred();
if (tasks) {
deferred.resolve(tasks);
return deferred.promise();
} else {
var promise1 = $.ajax({
url : "/server/tasks.json",
});
promise1.done(function(data) {
tasks = data;
setTimeout(function() {deferred.resolve(tasks)}, 5000);
})
return deferred.promise();
}
}
}
}();
Search WWH ::




Custom Search