How can I convert an onload promise into Async/Await
TypeScript now supports asynchronous functions for engines that have native support for ES6 generators, e.g. Node v4 and above. Asynchronous functions are prefixed with the async keyword; await suspends the execution until an asynchronous function return promise is fulfilled and unwraps the value from the Promise returned. - Source
async function getWorkbookFromFile2(excelFile: File) { return new Promise<xlsx.IWorkBook>((resolve, reject) => { var reader = new FileReader(); reader.onload = (event: any) => { var data = event.target.result; var workbook = xlsx.read(data, { type: 'binary' }); console.log(workbook.SheetNames); resolve(workbook); }; reader.readAsBinaryString(excelFile); });}
Example consumption:
async function caller() { var workbook = await this.getWorkbookFromFile2(this.getFile()); // The 'workbook' variable is an IWorkBook...}