What's the difference between a Deferred object and its own promise object? What's the difference between a Deferred object and its own promise object? javascript javascript

What's the difference between a Deferred object and its own promise object?


It creates a "sealed" copy of the deferred value, without the .resolve() and .reject() methods. From the documentation:

The deferred.promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request.

It's used when it doesn't make sense for the value to be modified. For example, when jQuery makes an AJAX request it returns a promise object. Internally it .resolve()s a value for the original Deferred object, which the user observes with the promise.


When using the "promise" of a Deferred object the observers (objects waiting for resolve for exemple) dont have direct access to the Deferred object itself, so they can't call, for exemple, the method "Resolve" of that Deferred. It is a way of protecting the original Deferred.


With Deferred, you can control its state set.

When it comes to the Promise, you can read state and maybe attach callback. get