Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


 
 

Promise: Life Beyond Callbacks

Posted by Sandeep Chanda on May 7, 2014

Callbacks have been the lifeline for the most part in the Ajax world. However, with increasing usage of JavaScript in the enterprise, developers are looking beyond simple callbacks--primarily because as the code grows, regular Ajax callbacks become clumsy and hard to maintain and extend.

Learn more about how Promise (Promise/A+ specification) and jQuery deferred objects show you the better side of JavaScript programming.

jQuery deferred objects and the Promise/A+ specifications are slightly different, with Promise/A+ being more canonical. jQuery deferred is a proxy for asynchronous future events, whereas promise allows for state inspection. The Promise specification is completely immutable.



A typical Ajax with a callback function looks like the following code snippet:

The most frequent issues with callback are the conflicts with DOM ready state and rendering. The same code written in Promise is more elegant. Promise provides the done(), fail() and always() event handlers in an orderly fashion.

You can also use Promise to chain callbacks with the help of the then() method as shown below:

Another significant feature of Promise is the ability to combine multiple service calls with the when() function as shown below:

The Promise specification is the recommended way of writing JavaScript in enterprise scale applications, allowing you to easily write clean and maintainable code.

TAGS:

JavaScript, jQuery, Ajax


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap