0

I am creating a reader for parsing an excel file. Below is my async function.

let readExcelhelper2 = async function () {
        console.log("readExcelHelper2 - start");
        //Parsing the excel file
        let reader = new FileReader();
        reader.readAsBinaryString(file);
        let didErrorOccur = false;
        console.log("readExcelHelper2 - mid");
        reader.onload = await function () {
            console.log("reader.onload");
            let data = reader.result;
            let workbook = XLSX.read(data, {type: 'binary'});
            let wsList = [];
            workbook.SheetNames.map(function (sheetName) {
                wsList.push(workbook.Sheets[sheetName]);
            });
            //console.log(wsList);
            if (wsList.length !== 1) {
                errorState['isError'] = true;
                errorState['listOfErrors'].push('The excel file should have exactly one sheet!');
                didErrorOccur = true;
            }
            if (didErrorOccur)
                return;

            readExcelHelper(wsList[0]);
        };

        console.log("readExcelHelper2- end");
        console.log(errorState);
        return {'classifiers': classifiers, 'errorState': errorState};
    };

This is the console log when readExcelHelper2 is called:

readExcelHelper2 - start
readExcelHelper2 - mid
readExcelHelper2- end
//A bunch of other things
reader.onload

Shouldn't it await on reader.onload to complete? Why is it not doing that?

Priyansh Agrawal
  • 330
  • 2
  • 19
  • 1
    `reader.onload = await function () {`. The `await` here does exactly nothing. You can await from expressions returning promises. Which means you should start by finding a function returning a promise (maybe your `FileReader` can but you don't tell us what it is). – Denys Séguret Jul 23 '18 at 07:56
  • @DenysSéguret Could you please elaborate? – Priyansh Agrawal Jul 23 '18 at 07:59
  • @DenysSéguret are you suggesting that I wrap the content in reader.onload into a promise? – Priyansh Agrawal Jul 23 '18 at 08:00
  • 1
    Wrapping the loading in a promise is a possible solution. see https://stackoverflow.com/questions/34495796/javascript-promises-with-filereader – Denys Séguret Jul 23 '18 at 08:00
  • 1
    JavaScript's async/await is a convenience wrapper for Promises, which is not what your function returns. Even then, your code requires some tweaking before async/await works the way you want it to. – Eugen Timm Jul 23 '18 at 08:03
  • you can call a function against onload.reader. something like this `onload.reader = returnReader();` Then Define it elsewhere returning a promise. `function returnReader(){ return new Promise(res,rej){ //there you can do what you want to do. } }` – Nouman Dilshad Jul 23 '18 at 08:10
  • @NoumanDilshad I AM calling a function against reader.onload. It's an anonymous function but a function nonetheless. – Priyansh Agrawal Jul 23 '18 at 08:11
  • Since the question is locked, I created a codepen for you: https://codepen.io/anon/pen/MBpQwd I also cleaned up some of your code – Eugen Timm Jul 23 '18 at 08:19
  • Thank you so much guys for all the help I received. I've been able to make this work by returning a promise from readExcelHelper2 and wrapping it around reader.onload by reading npm's and quentin's answer on this question(https://stackoverflow.com/questions/51473622/async-and-await-on-mysql-call-in-node-js). And I totally understand why the question was marked as duplicate. – Priyansh Agrawal Jul 23 '18 at 10:51

0 Answers0