4

What is the difference between defer object promise and promise from $resource service?

I know, that in some cases one uses $q service to create deferer, then resolve response and return promise.

Others in the same time might do something like return $resource(...).get().$promise;.

What is the diff. between those two approaches?

Eugene
  • 4,352
  • 8
  • 55
  • 79

1 Answers1

3

The promise returned from $resource is one someone initially used $q.defer() (or the newer more modern promise constructor) to create.

That someone is the $http service used internally inside $resource - you are using a promise they created for you.

Typically, you only need to use a $q.defer or the promise constructor at the lowest level of your code when working with async - otherwise you're typically better off using promise chaining. Otherwise you end up with the explicit construction anti-pattern.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • So to simply put it http and q service, or more advanced resource service with chained promise call, right? As I understand your answer the http and q are just leftovers, that some developers still use? – Eugene Jun 12 '15 at 10:42
  • @Eugene no, they're lower level primitives. `$resource` for rest management is built over `$http` for HTTP calls which is built over `$q` for promises and `XMLHTTPRequest` for web calls. Certainly not leftovers - there are lots of use cases for $http without $resource (for calls to non-rest services or a different style of calls for rest services) and a lot of use cases for $q without $http (for non http APIs - for example $timeout returns a promise). – Benjamin Gruenbaum Jun 12 '15 at 10:51
  • Okay. So just different usage scopes. – Eugene Jun 12 '15 at 11:01