1

I found 2 variation like 1. deferred.resolve(); 2. deferred.resolve(data);

after service response. What is the difference between 2. Which one I have to use strictly.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
Harish Menda
  • 71
  • 1
  • 2
  • 13
  • 1
    In one you're passing some data. You should use it when you need to pass some data. – Dave Newton Apr 06 '16 at 19:34
  • 1
    The first one resolve the promise without sending any data the the resolv function, the second one send `data` to the resolved function – Alon Eitan Apr 06 '16 at 19:34

2 Answers2

7

First scenario:

function getPromise() {
    var deferred = $q.defer();
    deferred.resolve();

    return deferred.promise;
}

getPromise().then(
    function(data) {
        console.log(data); // Output "undefined"
    }
);

Second scenario:

function getPromise() {
    var deferred = $q.defer();
    var data = "hello";
    deferred.resolve(data);

    return deferred.promise;
}

getPromise().then(
    function(data) {
        console.log(data); // Output "'hello'"
    }
);

So it all about on how you resolve the promise, with or without passing information to the function in the resolved promise.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
4

When you call a service, it mean that you're waiting for a server's response. The response could be a simple success or some data.

myServiceFunction = function() {
    var deferred = $q.defer();
       $http.post/get(myURL,someData/*not mandatory*/)
          .success(function(response){ // you get into this function when everything goes right
               deferred.resolve(response); //your sending back the server's response to your controller (or whatever has called this function)
        })
         .error(function(response){
              deferred.reject(response); // your also sending back the server's response but using reject means that something gone wrong on server call
    })
    return deferred.promise; // don't forget to send the promise!!
}

In the example above I'm sending the response server back to the one which has called the service function.

But if you just need to know that the request succeeded then a simple deferred.resolve() is enough (and deferred.reject() - to tell that it didn't succeed).

To make it shorter, here's what you should know:

  • deferred.resolve() - means request succeeded
  • deferred.reject() - request failed
  • deferred.resolve(data) - means request succeeded and here's some data
  • deferred.reject(data) - request failed and here;s some data

Hope it's clear enough

Chris Schaller
  • 13,704
  • 3
  • 43
  • 81
IsraGab
  • 4,819
  • 3
  • 27
  • 46
  • 1
    Don't forget to add `return deferred.promise;` – Alon Eitan Apr 06 '16 at 19:56
  • 1
    Also be sure to read [Is this a “Deferred Antipattern”?](http://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg Apr 06 '16 at 21:03
  • While this will work, I +1 the anti-pattern, my goto reference for deferrals when starting out was this: https://www.codelord.net/2015/09/24/%24q-dot-defer-youre-doing-it-wrong/ – Chris Schaller Mar 10 '20 at 14:37