1

I found this plnkr link on the web but I need use it with 2 or 3 more ajax calls which doesn't require an argument from the first ajax call. How can I do it with error handling?

var app = angular.module("app", []);

app.service("githubService", function($http, $q) {

  var deferred = $q.defer();

  this.getAccount = function() {
    return $http.get('https://api.github.com/users/haroldrv')
      .then(function(response) {
        // promise is fulfilled
        deferred.resolve(response.data);
        return deferred.promise;
      }, function(response) {
        // the following line rejects the promise 
        deferred.reject(response);
        return deferred.promise;
      });
  };
});

app.controller("promiseController", function($scope, $q, githubService) {

  githubService.getAccount()
    .then(
      function(result) {
        // promise was fullfilled (regardless of outcome)
        // checks for information will be peformed here
        $scope.account = result;
      },
      function(error) {
        // handle errors here
        console.log(error.statusText);
      }
    );
});

http://plnkr.co/edit/kACAcbCUIGSLRHV0qojK?p=preview

9
  • Give us more detail about different ajax calls Commented Jun 6, 2016 at 7:48
  • @WorkWe I mean more, not different, I have fixed the question now. Commented Jun 6, 2016 at 7:50
  • you could use $q.all to wait till all promise get resolved like $q.all([promise1, promise2]).then(funtion(data){ //success }) Commented Jun 6, 2016 at 7:53
  • 1
    also this code is using an anti-pattern, $http returns a promise so there is no reason to wrap it inside a deferred. Commented Jun 6, 2016 at 7:58
  • 1
    @PankajParkar is correct and honestly a quick google for $q.all example gives what you need ( First result on google : jsfiddle.net/jsengel/mc3p01nb ) Commented Jun 6, 2016 at 7:59

3 Answers 3

1

You can use $q.all

var promises=[
$http.get(URL1),
$http.get(URL2),
$http.get(URL3),
$http.get(URL4)
];

$q.all(promises).then(function(response){
console.log('Response of Url1', response[0]);
console.log('Response of Url2', response[1]);
console.log('Response of Url3', response[2]);
console.log('Response of Url4', response[3]);
}, function(error){

});

I have forked your plunkr with $q

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

Comments

1

First you should make deferred variable local for each ajax call you want to return a promise. So, you have to create 2-3 functions (as many as your ajax calls) and keep them in an array. Then you should use:

$q.all([ajax1,ajax2,ajax3]).then(function(values){
    console.log(values[0]); // value ajax1
    console.log(values[1]); // value ajax2
    console.log(values[2]);}); //value ajax3

example:

function ajax_N() {
   var deferred = $q.defer();

    http(...).then((response) => {
      deferred.resolve(response);
   }, (error) => {
      deferred.reject(error);
   });

   return deferred.promise;
}

$q.all([
        ajax_1,ajax_2,ajax_3
     ]).then(function(values) {        
          console.log(values); 
          return values;
        });

Comments

0

Use $q.all in this case. It will call getAccount and getSomeThing api same time.

var app = angular.module("app", []);

app.service("githubService", function($http, $q) {

  return {

    getAccount: function () {
      return $http.get('https://api.github.com/users/haroldrv');
    },

    getSomeThing: function () {
      return $http.get('some thing url');
    }
  };

});

app.controller("promiseController", function($scope, $q, githubService) {

  function initData () {
    $q.all([githubService.getAccount(), githubService.getSomeThing()])
      .then(
        function (data) {
          $scope.account = data[0];
          $scope.someThing = data[1];
        },
        function (error) {

        }
      );
  }
});

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.