Jquery facebook promise

Hi all,
what’s the difference between this
1)


 var ready = $.Deferred();
  window.FBReady = ready.promise();
  window.fbAsyncInit = function() {
    FB.init({
      appId      : window.FB_APP_ID, // App ID
      channelUrl : '//' + window.location.host + '/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true,  // parse XFBML
      frictionlessRequests: true
    });

    // Make sure the user is logged in and redirect if needed
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        ready.resolve(response);
      } else {
        ready.reject(response);
      }
    });
  };

  window.FBReady.done(function(response){
   //do something
  }).fail(function(response){
    //do something
  });

and
2)


var ready = $.Deferred();

  window.fbAsyncInit = function() {
    FB.init({
      appId      : window.FB_APP_ID, // App ID
      channelUrl : '//' + window.location.host + '/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true,  // parse XFBML
      frictionlessRequests: true
    });

    // Make sure the user is logged in and redirect if needed
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        ready.resolve(response);
      } else {
        ready.reject(response);
      }
    });
  };

 ready.done(function(response){
   //do something
  }).fail(function(response){
    //do something
  });

imo the promise is not necessary
but I’ve a doubt ^^

Can you enlight me, please ?

Hi there,

Good question!

A Deferred object is a Promise with methods that allow its owner to resolve or reject it.
When you call the Deferred’s promise() method, you create a ‘pure’ Promise which is identical to the Deferred, except that the resolve() and reject() methods are missing.
This is important for purposes of encapsulation, for example if you wish to return a Promise from a function, and only allow the caller to read its state or to attach callbacks to it.

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.

So a Promise is 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.

To bring this back to your original question, I don’t think it makes any difference in your code if you attach your callbacks to the Deferred or to its Promise.
However, based on what I have written above, I would prefer to use method 1.

If anyone else has any opinions, I’d be glad to hear them.