0

It has come to me the following query. Consider the next line of code:

case A: 
  $('#box').promise().done(function(){alert('Okay');});

In the above case, the alert message will be shown up immediately since $('#box').promise() will return a resolved promise object and the done callback will fire immediately. Now, if we change the above code to:

case B:   
  $('#box').hide(5000);
  $('#box').promise().done(function(){alert('Okay');});

the done callback will fire after 5 seconds. In that case I cannot find out in which way jQuery recognizes that $('#box') in the second line in case B is different from $('#box') in case A and knows that .hide() method is already applied on it as to correctly waiting for firing the callback. There must be some inner jQuery procedure...

Hope to be comprehensible.

Unknown developer
  • 5,414
  • 13
  • 52
  • 100

1 Answers1

2

There must be some inner jQuery procedure...

Yes, there is. The animation queue is attached to the DOM element using .data(), and will be retrieved by the call to .promise() which defaults to await the animation queue. In your case A, it just happens to be empty, so that the promise is immediately fulfilled.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • It appends it using `jQuery._data()` not `$.fn.data()` if i'm correct – A. Wolff Feb 03 '16 at 10:14
  • @A.Wolff Correct, and I originally wanted to write "*using the same mechanism as `.data()`*" but found it too technical for this short and simple answer :-) – Bergi Feb 03 '16 at 10:23
  • Is there any way to see the contents of the animation queue using .data() method on $('#box')? – Unknown developer Feb 03 '16 at 10:26
  • @ILIAS: Not really, the animation queue (and other queues) are supposed to be private so that they don't collide with regular `.data()` usage. You might be able to see something using `$._data($('#box')[0], "fxqueue")` - otherwise, fire up your debugger and step through the `.promise()` call :-) – Bergi Feb 03 '16 at 10:45