jQuery Can Provide Queue-Based Promise Objects

Posted October 24, 2011 at 10:23 AM by Ben Nadel

Tags: Javascript / DHTML

Last week, I was looking up some Promise information in the jQuery API when I came across the .promise() fn-method. While I have worked with promises in the context of AJAX and explicit Deferred objects, it seems that jQuery provides implicit promise functionality around their queues and dequeuing functionality. This allows for promise objects to be returned for animations, which are, after all, nothing more than encapsulated "fx" queues.


 
 
 

 
  
 
 
 

By default, the animation methods in jQuery (animate(), slideDown(), slideUp(), etc.) return a reference to the jQuery object (in order to maintain method chaining). And traditionally, if you want to perform an action after an animation is complete, you have to provide some sort of callback as an argument or property of the animation configuration.

With the .promise() method, however, you can get a promise object that will be resolved once the animation is done. This provides for more readable code (in my opinion) with the option for multiple callback handlers. To see what I'm talking about, take a look at the following code. In this demo, we're going to hide and show a DIV and alter the "action text" once the animation has been completed.

  • <!DOCTYPE html>
  • <html>
  • <head>
  • <title>jQuery Can Provide Queue-Based Promise Objects</title>
  •  
  • <style type="text/css">
  •  
  • div.container > div.buffer {
  • background-color: #E0E0E0 ;
  • border: 1px solid #CCCCCC ;
  • height: 200px ;
  • padding: 10px 10px 10px 10px ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery Can Provide Queue-Based Promise Objects
  • </h1>
  •  
  • <p>
  • <a href="#" class="action">
  • <span class="intent">Hide</span> Div
  • </a>
  • </p>
  •  
  • <div class="container">
  • <div class="buffer">
  •  
  • Hello, I am a div! How you like me now?
  •  
  • </div>
  • </div>
  •  
  •  
  • <!-- Include scripts. -->
  • <script type="text/javascript" src="./jquery-1.6.4.js"></script>
  • <script type="text/javascript">
  •  
  •  
  • // Get a reference to some DOM elements.
  • var action = $( "a.action" );
  • var actionIntent = action.find( "span.intent" );
  • var container = $( "div.container" );
  •  
  • // Hook up the click action.
  • action.click(
  • function( event ){
  •  
  • // Prevent the real event - not a true link.
  • event.preventDefault();
  •  
  • // Check to make sure we're not currently animating
  • // the DIV.
  • if (container.is( ":animated" )){
  •  
  • // Let the animation continue before the action
  • // link becomes a viable action.
  • return;
  •  
  • }
  •  
  • // Toggle the container. When implementing this
  • // animation, get a PROMISE object for the animating
  • // DOM element so that we can attach completion
  • // handlers to it.
  • var promise = container.slideToggle().promise();
  •  
  • // When the animation is done, update the text within
  • // the action link.
  • promise.done(
  • function(){
  •  
  • // Set the text of the action intent.
  • if (container.is( ":visible" )){
  •  
  • actionIntent.text( "Hide" );
  •  
  • } else {
  •  
  • actionIntent.text( "Show" );
  •  
  • }
  •  
  • }
  • );
  •  
  • }
  • );
  •  
  •  
  • </script>
  •  
  • </body>
  • </html>

As you can see, we are using the .slideToggle() method for animation. However, after we call the slideToggle() method, we are chaining it with the promise() method:

  • var promise = container.slideToggle().promise();

This returns a promise object rather than the jQuery object. Once we have the promise object, we can start to attach completion handlers using the done() method. This allows us to invoke zero or more callbacks once the animation has completed.

The documentation for the .promise() method doesn't mention anything about fail() outcomes. In my testing, I found that calling stop() on an animation did not trigger a fail() callback handler. However, if I called stop() and included the jump-to-end parameter, the done() callbacks would be invoked. So, it looks as if the only viable callback collection will be that of a successful completion.

Using .promise() with jQuery animation is not revolutionary. It doesn't provide any completely new functionality. What it does provide is a way to define callbacks in an easier, more readable, more flexible way. Also, when you consider the $.when() method, queue-based promise objects make it much easier to coordinate multiple animations.




Reader Comments

Oct 24, 2011 at 11:11 AM // reply »
3 Comments

This is cool because other methods to deal with the timing would involve timeouts, but due to congestion in the single-threaded JS execution order events could trigger out of synch and this removes that problem. Cool!


Oct 25, 2011 at 8:57 AM // reply »
11,314 Comments

@Randy,

I think the animation queue still works with timeouts under the hood; but the nice thing about this is that it will work with custom made queues as well.


Jan 5, 2012 at 2:26 PM // reply »
43 Comments

I'm going to try to use this method with executeSql commands.

"After you're done dropping the table, create the table".
"After you're done creating the table, insert into the table".

http://stackoverflow.com/questions/8748473/jquery-promise-deferred


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools