Using jQuery Deferred Objects and Promises Instead of Callbacks

Home > Blogs > Developer Visual Studio / ASP.NET > Using jQuery Deferred Objects and Promises Instead of Callbacks

Using jQuery Deferred Objects and Promises Instead of Callbacks

Like This Blog 0 Dan Wahlin
Added by March 8, 2013

In a previous post jQuery Tip #7 – Consolidating jQuery Ajax Calls I talked about how Ajax calls can be consolidated into a JavaScript object to promote re-use and simplify maintenance. In that post I showed how the Revealing Module Pattern can be used to provide a more structured approach and how a callback can be passed into functions within the object. Once individual Ajax calls complete the callback can be invoked so that the caller is able to access the returned data.

The following code shows an example of a simple object named dataService that follows the Revealing Module Pattern and has several Ajax functions in it. Note that each function accepts a callback as a parameter and that the callback is invoked to return the data to the caller.

Although this code works fine, by forcing a callback to be passed as a parameter the parameter signature of each function is polluted somewhat. Even worse, no error callback is defined so if an error occurs the caller of the dataService object may be left in the dark which will more than likely leave the user in the dark as well if something goes wrong.

JavaScript libraries such as jQuery, Q.js, and others support the concept of a deferred object and allow access to a promise. For example, jQuery's getJSON()function automatically returns a deferred object/promise to callers. That means that the caller receives an object immediately after calling an asynchronous Ajax function and can use it to handle the success and error callback functionality themselves rather than relying on the function to invoke a callback as in the previous code.

The following code shows an improved version of the callback code shown earlier. Notice that no callback parameter is passed into the different functions and that the result of the getJSON() call (a deferred object) is returned instead.

Consumers of the dataService object can now call functions, handle the deferred object that's returned and then wire up success and error functions. An example of doing that is shown next:

These two calls handle the success callback but don't handle errors. To do that you can chain a fail() function onto the previous code as shown next:

By using deferred objects and promises you can handle asynchronous callbacks in a more standardized way and provide a better to process success and error situations. You can also clean up the parameter signature of any Ajax methods that you may call and re-use throughout an application.

Enjoy!
Dan Wahlin Microsoft MVP
.NET Developer Instructor
Interface Technical Training

Videos You May Like

Windows 10 Features and Navigation – December 1, 2015

0 114 1

In this recorded Windows 10 webinar from December 1,2015, Windows Instructor Steve Fullmer presents the navigation and some of the new features associated with Windows 10 including Sysinternals Tools for Windows Client, Windows core concepts, exploring Process Explorer as well as some of the features that are not yet ready for prime time but will … Continue reading Windows 10 Features and Navigation – December 1, 2015

JavaScript for C# Developers – September 24, 2014

0 487 3

Is JavaScript worth taking the time to learn if I’m a server-side .NET developer? How much of C# carries over to JavaScript? In this recorded video from Dan Wahlin’s webinar on September 24,2014, Dan answers these questions and more while also discussing similarities between the languages, key differences, and the future of JavaScript (ES6). If … Continue reading JavaScript for C# Developers – September 24, 2014

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 608 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

Share your thoughts...

Please fill out the comment form below to post a reply.