Skip to main content

Bug Tracker

Side navigation

#9203 closed bug (duplicate)

Opened May 09, 2011 08:28PM UTC

Closed April 23, 2012 11:03AM UTC

Last modified April 23, 2012 11:03AM UTC

beforeSend doesnt take into account promises

Reported by: me@quickredfox.at Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.6
Keywords: Cc:
Blocked by: Blocking:
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'

Attachments (0)
Change History (8)

Changed May 09, 2011 08:44PM UTC by timmywil comment:1

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/

Changed May 16, 2011 07:08PM UTC by me@quickredfox.at comment:2

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?

Changed May 16, 2011 07:46PM UTC by timmywil comment:3

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.

Changed May 16, 2011 08:10PM UTC by me@quickredfox.at comment:4

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.

Changed November 29, 2011 08:26PM UTC by fastfasterfastest comment:5

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.

Changed April 23, 2012 11:03AM UTC by jaubourg comment:6

resolution: wontfix
status: closedreopened

Changed April 23, 2012 11:03AM UTC by jaubourg comment:7

resolution: → duplicate
status: reopenedclosed

Changed April 23, 2012 11:03AM UTC by jaubourg comment:8

Duplicate of #10944.