How to use Promises with PapaParse?

thehelpofgreat picture thehelpofgreat · Jul 13, 2015 · Viewed 9.1k times · Source

PapaParse has an asynch callback function for its API. I was wondering how I can convert it to a promise. For eg:

Papa.parse(fileInput.files[0], {
    complete: function(results) {
        console.log(results);
    }
});

Any help would be appreciated!

Answer

user663031 picture user663031 · Jul 13, 2015

The basic pattern is

Papa.parsePromise = function(file) {
  return new Promise(function(complete, error) {
    Papa.parse(file, {complete, error});
  });
};

Then

Papa.parsePromise(fileInput.files[0]) .
  then(function(results) { console.log(results); });