gopal rao
gopal rao

Reputation: 293

AngularJS - Promise vs then?

I have 2 questions -

1) What is the difference between the "success" and "then"? example - $http.get(url).then vs $http.get(url).success?

2) A promise means something that would be executed in the future, fine. I am not able to wrap my head on how this is difference from a function call. a promise call is executed when we make call using "$q.defer.resolve()". Isn't this a function call? so why can't this be a normal function, and executed with any other function call like foo()? Am i missing something special that a promise does!! ?

Upvotes: 2

Views: 1268

Answers (2)

New Dev
New Dev

Reputation: 49590

.then returns a new promise.

.success returns the original promise. Another difference is that .success gets the response.data as an input parameter, whereas .then gets the response.

This is how it's implemented:

promise.success = function(fn) {
   promise.then(function(response) {
      fn(response.data, response.status, response.headers, config);
   });
   return promise;
};

You could use .success (and .error) to handle the respective outcome of $http, but without the ability to modify the data or to reject a promise (with return $q.reject()) given to the eventual consumer of the $http promise.

Here's an illustrative example of when one may use .success vs/with .then:

return {
  getData: function(p1){
    return $http.get(url, {p1: p1})
             .success($log.log) // can use generic functions since there is 
             .error($log.error) // no requirement to return data
             .then(function(response){
                // here can massage the data into the output format
                return response.data; 
             });
};

I recommend always using .then since it returns a new promise without .success and .error handlers; this avoids the chance of the API consumer using the $http-specific .success/.error thus inadvertently making the assumption that $http was used under the covers.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

What is the difference between the "success" and "then"?

then() takes a single argument: the http response object. success() takes 4 arguments: the data of the response, the status of the response, the headers of the response, and the http config object. success() is thus easier to use when all you care about is the data returned. As said by New Dev, the returned value is different: then() returns a new promise for whatever is returned from its callback and allows you to return a new value and thus build a promise chain, whereas success() returns the original promise.

A promise means something that would be executed in the future

No, not really. A promise is the result of something you execute now, but for which the result is not immediately available. Since JavaScript is single-threaded, you can't just block and wait for the result, because that would completely freeze the application. So you just register a callback function that will be called, asynchronously, when the result is available. For example:

$http.get('/some/url')

immediately sends an HTTP request. But to get the result, we need to wait for the bytes to travel to the server, for the server to handle the request and send back a response, and for the response to travel to the browser. This could take several seconds. So what $http.get() returns immediately is a promise: i.e. an object which represents a future result (or, once the result has been received, a result that was once future).

Upvotes: 4

Related Questions