Promise: Life Beyond Callbacks

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS