0

I have tried to use the solution at: How to use Promises with PapaParse? to use Papa.parse with a promise, instead of using the inbuilt callback function. Unfortunately my configuration requirement is more complex than the given example as follows

Papa.parsePromise = function(file) {
  return new Promise(function(complete, error) {
    Papa.parse(file, {
        delimiter: "",
        newline: "",
        download: true, 
        error,
        complete: function(results){
    var tArray=[];
    var data = results.data;
    var temp = data.length;
    var filledRowCounter=0;
    filePresentMarker=1;
        for(var i=0;i<temp;i++){
            var row = data[i];
            if (row[0]!==""){
            filledRowCounter++; 
            tArray.push(row);
            }
         }
}   
        });
  });
};


Papa.parsePromise("Data/tables.csv")
.then(function(results) { 
  console.log(results); 
  });

The code filling tArray works fine. However, nothing is passed back to the .then function, so nothing is output to the console.log

IanR
  • 31
  • 1
  • 5

1 Answers1

2

You're never calling the resolve callback of the promise in your code. However, when taking that code you found you should keep the logic within the new Promise constructor minimal, and when you have a "more complex configuration requirement" you should just put that extra logic in a then callback from which you can return a value:

function parsePromise(file) {
  return new Promise((resolve, reject) => {
    Papa.parse(file, {
      delimiter: "",
      ewline: "",
      download: true,
      complete: resolve,
      error: reject
    });
  }).then(({data}) => {
    var tArray = [];
    var filePresentMarker = 1;
    for (const row of data) {
      if (row[0] !== "") {
        tArray.push(row);
      }
    }
    var filledRowCounter = tArray.length;
    return tArray; // or something
//  ^^^^^^^^^^^^^^
  })
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375