title: call function synchronously inside an angular for each
body: <p>I'm using ngCordova File Transfer plugin in an ionic project to download set of images from urls. Here is the code i'm using for that.</p>
  
  <pre><code>// Save a image file in a given directory
  $scope.saveImage = function(dir,imgUrl,imageName) {
  
      var url = imgUrl;
      var targetPath = cordova.file.dataDirectory+ dir+"/" + imageName;
      var trustHosts = true;
      var options = {};
  
      // Download the image using cordovafiletransfer plugin
      $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
      .then(function(result) {
          $scope.loadedCount ++;
          $ionicLoading.show({template : "&lt;ion-spinner class='spinner-energized'&gt;&lt;/ion-spinner&gt;&lt;p&gt; Downloading pages : "+ $scope.loadedCount+" of "+ $scope.pages.length+ "&lt;/p&gt;&lt;p&gt;Please wait...&lt;/p&gt;&lt;p&gt;&lt;button class=\"button button-block button-positive\"&gt;continue in background&lt;/button&gt;&lt;/p&gt;"});
          if($scope.loadedCount == $scope.pages.length) {
              $ionicLoading.hide();
              $scope.showDownloadSuccessAlert = function() {
                  var alertPopup = $ionicPopup.alert({
                      title: 'Success!',
                      template: "Your magazine successfully downloaded. You can view it on Downloads!"
                  });
              };
              $scope.showDownloadSuccessAlert();
          }
      }, function(err) {
          alert(JSON.stringify(err));
      }, function (progress) {
          if($scope.loadedCount &gt; 80) {
          }
      });
  };
  
  // Download the current magazine
  $scope.downloadMagazine = function() {
      if($rootScope.user.user_id == undefined) {
          $scope.showLoginAlert = function() {
                  var alertPopup = $ionicPopup.alert({
                      title: 'Oops!',
                      template: "Your must login to download magazines"
                  });
              };
          $scope.showLoginAlert();
          return;
      }
  
      document.addEventListener('deviceready', function () {
  
          var dirName = $rootScope.currentIssue.slug+'_VOL_'+$rootScope.currentIssue.vol+'_ISU_'+$rootScope.currentIssue.issue;
  
          // First create the directory
           $cordovaFile.createDir(cordova.file.dataDirectory, dirName, false)
          .then(function (success) {
              var count = 1;
              $scope.loadedCount = 0;
              angular.forEach($scope.pages, function(value, key) {
                 var imgName = count+".png";
                 $scope.saveImage(dirName,value.link,imgName); // Then save images one by one to the created directory.
                 count++;
              });
  
          }, function (error) {
              // Directory already exists means that the magazine is already downloaded.
              $scope.showDownloadedAlert = function() {
                  var alertPopup = $ionicPopup.alert({
                      title: 'Why worry!',
                      template: "Your have already downloaded this magazine. You can view it on downloads"
                  });
              };
              $scope.showDownloadedAlert();
          });
  
      }, false);
  };
  </code></pre>
  
  <p>})</p>
  
  <p>Problem here is that program try to download everything at once without waiting for previous one to finish. How to wait for one file to finish downloading and then start the other?</p>
  
  <p>Thanks</p>