异步等待无法正常工作

时间:2018-07-23 07:55:52

标签: javascript async-await

我正在创建用于读取Excel文件的读取器。下面是我的异步函数。

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};
    };

这是调用readExcelHelper2时的控制台日志:

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

是否要等待reader.onload完成?为什么不这样做?

0 个答案:

没有答案
相关问题