// a deferred object is for holding and calling a queue of success/failure/complete callbacks
// an ajax call allows us to give success, error and complete handlers. However, it doesnt allow for a case where we want to call a success handler based on some condition. For that we have to program ourselves some kind of if else block. Moreover, we can not change the condition at any time, that is once the ajax call is made the handler is set, if in between the ajax call this condition changes, ajax can not accommodate that.
// deferred objects allows multiple callback registrations and also allows for the registration at any time i.e during creation, execution or after completion
var deferred = $.Deferred();
var f = function(arg) {
$("#output").append(arg + "<br/>");
};
$(function() {
$("#btnResolve").click(function() {
if (deferred.state() === "pending") f(deferred.resolve().state());
});
$("#btnReject").click(function() {
if (deferred.state() === "pending") f(deferred.reject().state());
});
$("#btnState").click(function() {
f(deferred.state());
});
//Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished.
$("#btnPromise").click(function() {
$('p').append("Started..");
$('.demo').each(function(i) {
$(this).fadeIn().fadeOut(1000 * (i + 1));
});
$('.demo').promise().done(function(){
$('p').append("Finished..");
});
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.