0

I have an api ('rest/latest/testruns/16543558') the id is test run ids . I want to make a call with different ids in oneshot and store the value in object for ng-repeat in my view.

$http
  .get('rest/latest/testruns/' + testplanid)
  .success(function (response) {
    $scope.testruns = response;
  })
  .error(function (data, status) {

  });
2
  • Where you are calling the $http in your controller? Commented Mar 17, 2017 at 13:03
  • Just to clear up your goal here, you have an array of ids and for each you want to make a call to your REST endpoint and have all values returned at once so you can store the results in some value? If that is the case, you should look up Promise.all - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 17, 2017 at 13:11

3 Answers 3

1

can use $q.all to send multiple http requests at once.

 var ids = ['16543558', '16543559', '165435510']
 var fullArr = [];

 for (id in ids) {
    fullArr.push($http.get("rest/latest/testruns/" + id))
 }

 $q.all(fullArr).then(function(arrayOfResults) {
    //return the response of all equest
 });
Sign up to request clarification or add additional context in comments.

Comments

1

You could use $q.all like below to achieve the similar behavior

var ids=[1,2,3,4,5], promises;

promises = ids.map(function(v){
return $http
  .get('rest/latest/testruns/' + v)
});

$q.all(promises).then(function(responses){
  // responses[0]  => id 1
  // responses[1]  => id 2
  // responses[2]  => id 3
  // responses[3]  => id 4
  // responses[4]  => id 5
})

Comments

0

You can use $q.all to achieve the multiple promise calls.

HTML code :

<div ng-app>
  <div ng-controller="PromiseCtrl">
      {{result}}
  </div>
</div>

Controller function :

function PromiseCtrl($scope, $q, $timeout) {
    // Our promise function with its delay as argument
    var getPromise = function(delay) {
        // Creates a Deferred object
        var deferred = $q.defer();

        $timeout(function() {
            // Resolve the promise at the end of the delay if said delay was > 0
            if(delay > 0) {
                deferred.resolve("Success");
            } else {
                deferred.reject("Fail");
            }
        }, delay);

        // The promise of the deferred task
        return deferred.promise;
    };

    // Init
    $scope.result = "Waiting";

    /*
     * Combines multiple promises into a single promise
     * that will be resolved when all of the input promises are resolved
     */
    $q.all([
            getPromise(1000),
            getPromise(2000),
            getPromise(3000) // <--- Try something less than 0
        ]).then(function(value) {
        // Success callback where value is an array containing the success values
        $scope.result = value;       
    }, function(reason) {
        // Error callback where reason is the value of the first rejected promise
        $scope.result = reason;
    });
}

Please find the plnkr : https://jsfiddle.net/samirshah1187/Lpach13a/

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.