Bug Tracker

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#9203 closed bug (duplicate)

beforeSend doesnt take into account promises

Reported by: me@… Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.6
Keywords: Cc:
Blocked by: Blocking:


var request = $.ajax({ beforeSend: function() { return false } });

request is "false" and therefore not a promise as expected.

request.done() Uncaught TypeError: Object false has no method 'done'

Change History (8)

comment:1 Changed 6 years ago by timmywil

Component: unfiledajax
Priority: undecidedlow
Resolution: wontfix
Status: newclosed

Returning false in beforeSend will cancel the request at all times so there is no jqXHR/promise to return. I see the point that it's no longer a promise and you can't extend it, but the code itself is completely pointless. If you just want a promise, use jQuery.Deferred or the new $.fn.promise() method.

If the return false is conditional as it should be, everything is fine: http://jsfiddle.net/timmywil/r6vtp/

comment:2 Changed 6 years ago by me@…

In your example, if "blar" is falsey, $.ajax() still returns a boolean.

In my understanding... If a function claims to return a promise - like $.ajax() claims to return a jqXHR - then it should ALWAYS return a promise. Otherwise it's quite pointless and unreliable to use $.ajax() calls in any other way that how we did before $.Deffered() came along.

If no jqXHR is created at that time, then shouldn't a pre-rejected() promise be returned instead of a boolean?

comment:3 Changed 6 years ago by timmywil

That's how it would work in any valid use case. I think you meant to say if blar is true, it returns a boolean, but you missed the point. Always returning false in the beforeSend is NOT a valid use case and there is absolutely no reason that should ever ever be done.

comment:4 Changed 6 years ago by me@…

Ok, I see that you do not understand what I'm saying at all and that is probably due to the fact that I named this ticket "beforeSend doesnt take into account promises" when in fact I'm talking about the fact that $.ajax() does not ALWAY return a promise, regardless of beforeSend's returned output. beforeSend() is ok... $.ajax() is broken.

Sorry to bother you, will investigate more, provide some tests and re-open a ticket titled "$.ajax() does not always return a jqXHR"


NB: It's not about my use-case, its about returning what you promise to return. This is my final comment in this thread.

comment:5 Changed 5 years ago by fastfasterfastest

I agree with me@… - I think $.ajax() should always return a promise (as specified by the documentation http://api.jquery.com/jQuery.ajax/ where it is stated that $.ajax returns jqXHR). The current behavior of $.ajax returning a boolean false when beforeSend returns false, is inconsistent and it prevents one from using the very nice interface of promises, if one specifies a beforeSend callback. If one specifies a beforeSend callback one can currently e.g. NOT write:

$.ajax('/echo/html', {
   beforeSend: function() {
      return confirm('Do you want to echo?');
.fail(function(){alert('Did not echo');});

because if beforeSend returns false, $.ajax() returns false (and NOT a promise), and thus .done() will be an error.

I would argue that if beforeSend returns false, then $.ajax() should return a promise that has been rejected. And I urge to reconsider your rejection of this bug report.

The alternative must be to update the documentation to specify that $.ajax() sometimes returns a jqXHR and sometimes a boolean.

comment:6 Changed 5 years ago by jaubourg

Resolution: wontfix
Status: closedreopened

comment:7 Changed 5 years ago by jaubourg

Resolution: duplicate
Status: reopenedclosed

comment:8 Changed 5 years ago by jaubourg

Duplicate of #10944.

Note: See TracTickets for help on using tickets.