0

I am new to angularjs, currently I am facing one issue, which is like -

I am calling one service that is -

       if($scope.tab === "1"){
          $scope.loadreports();
          $scope.loaddocumentForHighlighting();
        }
      $scope.loadreports = function(){
        uploadService.loadReports(uploadService.currentFileName, $scope.documentType, jdCurrentFileName)
                .then(function (response) { uploadservice.setreprtdata(response) },function (error) {
                  $scope.loadingReports = false;
                  $scope.errorMessage = error.status + " : " + error.statusText;
                  if (error.status === 401) {
                    loginService.authenticationError();
                  }
    }
Now ,

$scope.loaddocumentforhighlighting = function(){
    uploadService.getDocumentAsHTML($scope.documentType, uploadService.currentFileName)
              .then(function (data) {}, .finally(function ( console.log(uploadservice.getreportdata()) ) {
                $scope.showDocumentReloadPanel = false;
              });
}

Now,here what is happening When first call is of the loadreports then it takes some time to get the response from server. But in that method I have one set and that I want to use in the second method. But before that the second methods gets executed. So, get in that method gives me undefined. But the thing is that I get the response of the first method as well. But Not in the time. SO, How can I resolve this issue ?

ganesh kaspate
  • 1
  • 9
  • 41
  • 88
  • Possible duplicate of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Estus Flask Apr 04 '18 at 13:51
  • The wording you used is a bit hard to understand, but i think what you need are promises, which is implemented in angular using $q. – Yftach Apr 04 '18 at 13:51

1 Answers1

1

These methods should return promises:

$scope.loadreports = function(){
  return uploadService.loadReports(...).then(...);
}

$scope.loaddocumentforhighlighting = function(){
  return uploadService.getDocumentAsHTML(...).then(...);
}

And be chained like:

$scope.loadreports().then(function () {
  return $scope.loaddocumentForHighlighting()
});
Estus Flask
  • 206,104
  • 70
  • 425
  • 565