0

The function "classTableData" is called on button click and my allData is undefined.It processes the next line of code without waiting for the results and hence my "allData" is undefined.Can someone help on this?

function classTableData(){
     loadStudentPDFData().then(function (results) {
                  studentPDFData = results.pdfData;
                });
        var allData = studentPDFData;
        $log.log("AllData"+allData.length);

    }

function loadStudentPDFData() {
  var deferred = q.defer();
   var core5PDFData=angular.copy(core5InstructionPlanner);
       core5PDFData.loadPDFSection('all', function() {
     deferred.resolve({pdfData:core5PDFData.dataSource('all').data()});
      });
    return deferred.promise;
  }
VSP
  • 33
  • 8
  • Put the log in the callback. Promises don't make your code synchronous, you still have to wait for the result. – Bergi Nov 04 '17 at 16:28

2 Answers2

0

loadStudentPDFData return a promise.

The resolved promise returns the object {pdfData:core5PDFData.dataSource('all').data()}.

While you have put studentPDFData = results.pdfData; inside the resolve callback, you have another two lines outside it.

    var allData = studentPDFData;
    $log.log("AllData"+allData.length);

This results in studentPDFData being undefined when it is assigned to allData resulting in the logging of null. If you push these two lines inside the callback, it should work.

i.e.

 loadStudentPDFData().then(function (results) {
      studentPDFData = results.pdfData;
      var allData = studentPDFData;//you don't need this variable
      $log.log("AllData"+allData.length);
 });

Javascript is inherently asynchronous and Promises are an integral part of it. A promise is used to complete actions based upon results which might come to the code later (ex. database calls, or http requests etc.).

You can read more about promises here and about the q library here.

Parijat Purohit
  • 921
  • 6
  • 16
  • but i need allData to be processed outside this function too,how to achieve that? – VSP Nov 04 '17 at 17:52
  • Why do you need it "outside" this function exactly? the function is a mere wrapper over the promise object. It simply shows that the response is received from the loadStudentPDFData promise. Can you show in an example where you need this variable outside? – Parijat Purohit Nov 04 '17 at 18:09
  • i should return the processed allData from the classTableData function. var classTableDataForPDF = classTableData(); classTableDataForPDF gives undefined value. – VSP Nov 05 '17 at 06:56
  • In that case, even the current function needs to return a promise which needs to be consumed later in a similar fashion. JavaScript is different :) – Parijat Purohit Nov 05 '17 at 07:08
0

Perform the task within .then()

loadStudentPDFData()
.then(function (results) {
  $log.log("AllData" + results.pdfData.length);
  return results.pdfData
});

if a .then() is chained to classTableData return loadStudentPDFData() from classTableData() function

function classTableData() {
  return loadStudentPDFData()
         .then(function (results) {
           $log.log("AllData" + results.pdfData.length);
           return results.pdfData
         });
}
guest271314
  • 1
  • 15
  • 104
  • 177