3

I am trying to figure out promises... What is the difference between simply making an $http request, then acting upon whether is what successful (.success()) & it failing (.fail()), in comparison to a promise (q = $q.defer()) with q.resolve()?

1
  • I'm not sure I understand your question. How are you making a $http request with $q.defer? If you don't make a request, then that's obviously the difference. Commented Jul 2, 2015 at 19:57

2 Answers 2

3

$http itself uses $q. These both services are not operating at the same level.

$http.get(url) returns a promise that can be resolved or rejected. It means that you are using a promise delivered by a service ($http). Internally, $http will call $q.defer(), then $q.resolve() or $q.reject(). This will call either your .success() or .fail() method.

$q is a service to create your own promise.

Using $q is a great way to learn about promises but in your case, $http is already doing that work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

The $http legacy promise methods success and error/fail have been deprecated. docs.angularjs.org/api/ng/service/$http#deprecation-notice
Yes, success and error/fail have been deprecated in favor of then. But the core answer stays the same. $http returns a promise and $q "creates" a promise. Within then() you still have two callback functions for success and fail. $http
0

Nothing. $http methods return promises. In short, they can also be chained with then. success and fail are sugar for resolve-only and reject-only thens.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.