title: Difference between $q.resolve() vs deferred.resolve() - AngularJS
body: <p>I need to return a customized response in a promise, from an <em>$http</em> call, so I can chain further calls. I have two implementations available. Can someone explain the difference between two, and argue if one is better?</p>
  
  <p><em>In fooService.js</em></p>
  
  <p><strong>Implementation #1</strong></p>
  
  <pre><code>function foo() {
      var deferred = $q.defer();
      return $http.get('some-http-url')
          .then(function(response) {
              var data = response.data.Data;
              // Some manipulation on data
              deferred.resolve(data);
              return deferred.promise;
          });
  }
  </code></pre>
  
  <p><strong>Implementation #2</strong></p>
  
  <pre><code>function foo() {
      return $http.get('some-http-url')
          .then(function(response) {
              var data = response.data.Data;
              // Some manipulation on data
              return $q.resolve(data);
          });
  }
  </code></pre>
  
  <p><em>And then in FooController.js</em></p>
  
  <pre><code>fooService.foo().then(function(response) {
      // Do something
  });
  </code></pre>
  
  <p><strong>P.S. Some links, that can give me a better understanding are welcome.</strong>
  <br><br><br>
  <strong><em><h1>**************UPDATE 4th October, 2017**************</h1></em></strong></p>
  
  <p>If I modify my function foo() like this</p>
  
  <p><strong>Implementation #1</strong></p>
  
  <pre><code>function foo() {
      var deferred = $q.defer();
      if(/*some condition*/) {
          deferred.resolve('data');
          return deferred.promise;
      }
      else {
          deferred.reject('error');
          return deferred.promise;
      }
  }
  </code></pre>
  
  <p><strong>Implementation #2</strong></p>
  
  <pre><code>function foo() {
      if(/*some condition*/)
          return $q.resolve('data');
      else
          return $q.reject('error');
  }
  </code></pre>
  
  <p>Which is the preferred way?</p>