success and error function in the controller for a service

17.9k views Asked by At

I have the following code in a service and I am calling fetchData function from the controller.

Service

app.service("geturl", function($http) {
    urllist = [];
    geturl.fetchData = function() {
        var data = [];
        for (i = 0; i < urllist.length; i++) {
          (function(index) {
            return $http.get(geturl.urllist[index], {
              timeout: 8000
            })
            .then(function(response) {
              data[index] = response.data;
            });
          }(i);
          return data;
        });
    };
});

I want to write the success and error function of $http.get in the controller since it is needed in the UI, how can I go about it?

2

There are 2 answers

5
nalinc On BEST ANSWER

..I want to write the success and error function of $http.get in the controller..

Usually, the .then() function takes two function arguments. The first argument is the success handler and the second as an error handler.

$http.get(url,options).then(function (response){
    //success handler function
    },function(error){
  //error handler function     
  })

Alternatively, you can specify the .success and .error functions separately.

$http.get(url,options).success(function (response){
    //success handler function
    }).error(function(error){
  //error handler function     
  })

UPDATE: From your code, it seems that you intend to return something from your service geturl and providing the callbacks there itself. This is not how it is supposed to be done. You should return a promise from your service .

   ... 
   app.service("geturl",function($http){
     ...
     getData:function(){
        return $http.get(url,options);
     }
     ...               
   });
   ...

and handle the success/error callbacks in the module where you are consuming the service

geturl.getData().success(function(){
//handle success
}).error(function(){
//handle error
})

In case you need to make multiple http requests, never ever use the for loop . Remember, everything is asynchronous and you are not guaranteed to get the response from one of the previous request before you make a new one. In such scenarios, you should use $q service. See @pankajparkar's answer for more details

6
Pankaj Parkar On

Seems like you want to return a data after all the ajax gets completed, You could achieve this by using $q.all()

Service

app.service("geturl", function($http, $q) {
    this.urllist = [];
    this.fetchData = function() {
        var data = [], promises = [];
        for (i = 0; i < urllist.length; i++) {
          (function(index) {
            var promise = $http.get(geturl.urllist[index], {
              timeout: 8000
            })
            .then(function(response) {
              data[index] = response.data;
            });
            promises.push(promise); //creating promise array
          }(i);
        };
        return $q.all(promises).then(function(resp){ 
            return data; //returning data after all promises completed
        });
    };
});