Book Image

Mastering JavaScript Promises

Book Image

Mastering JavaScript Promises

Overview of this book

Table of Contents (16 chapters)
Mastering JavaScript Promises
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The solution – introducing promises in jQuery


The solution to the preceding problem was finally delivered in jQuery 1.5 as the deferred object. Before the deferred concept was introduced in jQuery, the typical AJAX call was something like this:

$.ajax({
  url: "/testURL.com",
  Success: TheSuccessFunction,
  Error: TheErrorFunction
});

Can you guess what could be the output of this function? Yes, a single XMLHttpRequest object, which is quite expected for those who are still maintaining the apps built before jQuery 1.5.

Now, what dramatical change was introduced in jQuery 1.5. First of all, it's based on a specification of common JavaScript that defines common interfaces and can be extended as per the needs, and secondly, they are quite global and you can use these in similar services, such as Node.js.

After the addition of deferred objects in jQuery 1.5, the preceding code was rewritten like this:

var promise = $.ajax({
  url: "/testURL.com"
});
promise.done(TheSuccessFunction);
promise.fail...