I’m having a tough time wrapping my head around deferred objects in jQuery.
E.G,
I thought I could use the following syntax, but this is actually running both success and fail when a success happens. I thought fail would only run if the ajax call fails?
checkFoo(widget)
.success(step1, step2)
.fail(alert("failed"));
checkFoo is an AJAX call like so
function checkFoo(widget){
return $.ajax({
url: "foo.php",
data: widget,
format: json
});
}
Your code
calls
step1andstep2andalertimmediately, and passes their return values into thesuccessorfailmethods. Exactly like…calls
barand passes its return value intofoo.If you want to tell jQuery to call
step1andstep2on success, and do thealerton failure, you pass in function references: