Bug Tracker

Ticket #9203 (closed bug: duplicate)

Opened 3 years ago

Last modified 3 years ago

beforeSend doesnt take into account promises

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

Description

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

comment:1 Changed 3 years ago by timmywil

  • Priority changed from undecided to low
  • Resolution set to wontfix
  • Status changed from new to closed
  • Component changed from unfiled to ajax

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 3 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 3 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 3 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"

 http://jsfiddle.net/63GPC/

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 3 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?');
   }
})
.done(function(){alert('Echoed');})
.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 3 years ago by jaubourg

  • Status changed from closed to reopened
  • Resolution wontfix deleted

comment:7 Changed 3 years ago by jaubourg

  • Status changed from reopened to closed
  • Resolution set to duplicate

comment:8 Changed 3 years ago by jaubourg

Duplicate of #10944.

Note: See TracTickets for help on using tickets.