atulquest93 atulquest93 - 1 year ago 95
AngularJS Question

Make multiple Async Request using Angular Js Promises

I have an array of cities with their name and url .

$scope.cities = [{'name':'LKO','url': ''},{'name':'LK1O','url': ''}]

Now I need to make request to the url present in the city.. One after another as response of one request arrives.

I know this is possible using promises.
But, I'm not able to get the exact solution.

Answer Source

You can use recursive loop like in this example:

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


$scope.cities = [{'name':'LKO','url': ''},{'name':'LK1O','url': ''},
{'name':'LK21','url': ''}]
  //recursive function
  function getUrl(i){
      if (typeof $scope.cities[i]=='undefined')
      return; //last one 
      console.log("ajax to "+  $scope.cities[i].url);
      $http.get($scope.cities[i].url).then( getUrl(i+1) );
<script src=""></script>
<div ng-app="app" ng-controller="main">

PS. i changed url to this github question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download