$.Deferred().promise() in jQuery

One of the shiny stars of Deferred is its promises. What this method can do? Well, it returns an object, and with nearly the same interface as Deferred. However, there is a catch. It's there just to attach the callbacks and not to resolve or reject.

This is quite useful in some other conditions, say you want to call out an API. This will not have the ability to resolve or reject the deferred. Such code will eventually fail as the promise here does not have a method.

Try executing this code, save it as test.html and run the file:

<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.0.js"></script> <script> function getPromise(){ return $.Deferred().promise(); } try{ getPromise().resolve("a"); ...

Get Mastering JavaScript Promises now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.