deferred.promise() returns a Promise for that Deferred (since jQuery 1.5). The Promise lets other code attach handlers or read state(), but it hides methods that change the outcome — resolve, reject, notify, and their *With forms.
Syntax
Start with these common calls. Keep them short and readable.
Helper owns settle.ok chooses resolve or reject inside the timeout.
Return Promise. The caller never sees dfd.
Chain done/fail. Success prints Hi Alex.
Frequently Asked Questions
promise() returns a Promise tied to the same outcome as the Deferred. Callers can use then, done, fail, always, progress, state, pipe, and promise — but not resolve, reject, notify, or the *With variants.
Encapsulation. Only the owner should settle the operation. Returning promise() lets other code attach handlers or inspect state without forcing resolve, reject, or notify.
promise(target) attaches promise methods onto an existing object and returns that object. Useful when an API object should also act as a thenable Promise.
No. The Promise does not expose resolve, reject, or notify. Only the original Deferred can settle the work.
Similar idea — a future result — but jQuery’s Promise also supports done/fail/progress. In jQuery 3+, then() follows Promises/A+ semantics.
No. Each promise() view observes the same Deferred. Settling the Deferred once settles every promise derived from it.