Search code examples
javascriptpapaparse

Files don't parse in correct order


I am using PapaParse to parse multiple CSV files. The file selector has them in the order of A B C D E, but they aren't always parsed in that order. I understand that it is because PapaParse will finish up with one file before another, but it is crucial that I parse them in the order that they appear from the file selector i.e. alphabetical.

var Files = $('input[id="upload"]')[0].files;
var allProcessed = [], allDates = [];
for (i in Files)
{
  const date = new Date(Files[i].lastModified);
  allDates.push((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());

  Papa.parse(Files[i],
  {
    skipEmptyLines: true,
    complete: function(results)
    {
      allProcessed.push(results);
      if (allProcessed.length == Files.length)
      {
        console.log('all done');
      }
    }
  }
}


Solution

  • From the Papa Parse documentation:

    Papa.parse(file, config): doesn't return anything. Results are provided asynchronously to a callback function.

    So the parse order is not guaranteed. If you really need them to be parsed in order, you could start the next parse when one parse finishes.

    Here is an example of how you could chain the calls to Papa.parse(), assuming you have an array of files:

    const files = [ /*an array of files*/ ];
    let currentIndex = 0;
    
    function getNextFile() {
      return files.length == currentIndex? null : files[currentIndex++];
    };
    
    const config = {
      skipEmptyLines: true,
      complete: function(results) {
        allProcessed.push(results);
        parseNextFile();
      }
    };
    
    function parseNextFile() {
        const file = getNextFile();
        if (!file) {
          console.log('all done');
        } else {
          Papa.parse(file, config);
        }
    };
    
    parseNextFile();