1

I'm constructing elements from metadata and i need to set a calculated class for each element.

This is what I currently do,

var promisses =_.map(templates, function (tmpl) {
  return $http.get(tmpl.template, {
    cache   : $templateCache,
    // Generated class name is carried to the resolving function using the config
    classes : scope.generate_class(tmpl.columns)
  }).then(function (data) {
    if ( data.status != 200 )
      throw new Error('Failed to fetch template');
    var elm = angular.element(data.data);
    elm.addClass(data.config.classes);
    return elm;
  });
});

$q.all(promisses).success.... 

If I want to use success instead of then fir the $http bit (which evaluates in case of an error as well) how would i do that ? when using success the config is not carried on to the resolving function (only the data).

Thanks.

1
  • Eh... you really don't want to use .success here since it doesn't chain well. Commented Jun 20, 2014 at 8:45

1 Answer 1

1

From $http docs:

Returns a promise object with the standard then method and two http specific methods: success and error. The then method takes two arguments a success and an error callback which will be called with a response object. The success and error methods take a single argument - a function that will be called when the request succeeds or fails respectively. The arguments passed into these functions are destructured representation of the response object passed into the then method.

The response object has these properties:

  • data{string|Object} – The response body transformed with the transform functions.
  • status{number} – HTTP status code of the response.
  • headers{function([headerName])} – Header getter function.
  • config{Object} – The configuration object that was used to generate the request.
  • statusText{string} – HTTP status text of the response.

So you can pass the config like so:

.success(function(data, status, headers, config) {

Do not throw errors when using promises, if your server doesn't return an error code you can use q.reject to transform it to a rejection, also q.all promises doesn't have a success method:

var promisses =_.map(templates, function (tmpl) {
  return $http.get(tmpl.template, {
    cache   : $templateCache,
    // Generated class name is carried to the resolving function using the config
    classes : scope.generate_class(tmpl.columns)
  }).then(function(res) {
    if ( res.status != 200 ) {
      return $q.reject('Failed to fetch template');
    } else {
      var elm = angular.element(res.data);
      elm.addClass(res.config.classes);
      return elm;
    }
  });
});

$q.all(promisses)
  .then(function() { ... })
  .catch(function() { .. })
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Ilan. Wouldn't the status always be 200 if reached success ?
@haki it could be any of these success code 2xx / 304
I just followed your code I don't know what your server returns on error
I was just saying that if lets say the server returns with 404, it wont reach success, it will reach error.
Huh? You're returning a $q.reject from a .success? I'm not sure that does what you think it does.
|

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.