Bug Tracker

Opened 10 years ago

Last modified 10 years ago

#10944 closed bug

$.ajax does not always return an object implementing the Promise interface — at Initial Version

Reported by: fastfasterfastest Owned by:
Priority: low Milestone: 1.8
Component: ajax Version: 1.7.1
Keywords: 1.8-discuss Cc:
Blocked by: Blocking:


The documentation of $.ajax states "The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface...". This is not true in 1.7.1. As a consequence one cannot (safely) use the Promise programming model on the object returned by $.ajax.

If you use a beforeSend callback and the callback returns false, then $.ajax does not return an object that implements the Promise interface - $.ajax instead returns a boolean.

E.g., http://jsfiddle.net/67DJr/

$.ajax( '/echo/html' , { beforeSend: function(){ return confirm('Access resource?'); } })
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

The above code causes a javascript error if user clicks Cancel.

Per documentation, $.ajax should always return an object that implements the Promise interface. In the case of beforeSend returning false, and thus cancelling or preventing the call from being made, it seems to make sense for $.ajax to return a Promise object that has been rejected.

Change History (0)

Note: See TracTickets for help on using tickets.