Tuesday 11 April 2017

Console.log shows array but can't be returned

I'm new to working with promises (I'm using 'co' in node) so I'm not entirely sure what's failing with this code:

function* excelToJSON(excelFileNames) {
  var jsonData = [];

  for (let index = 0; index < excelFileNames.length; index++) {
    parseXlsx(excelFilesNames[index], function (err, data) {

      jsonData.push(data);
      console.log(jsonData); //***Shows data correctly
    });
  }

  console.log(jsonData); //***Empty array
  return yield jsonData;
}

It reads the file, converts it and, at least within the loop, it shows everything correctly, but once we get out of the loop the data seems to disappear. I've also tried to return one of the values from within the loop but that doesn't work either.



via Vonmood

No comments:

Post a Comment