I have a problem with asynchronism ...
I am trying to create a service in angular that gives me the data of an excel in json. I'm doing this with a library and it works well. my problem is that calling it as a service I leave it within a promise but within the flow there is a process that takes a bit and returns the data in undefined.
prueba(evt:any){
var data:any;
return new Promise((resolve, reject) => {
const target: DataTransfer = <DataTransfer>(evt.target);
if (target.files.length !== 1) throw new Error('Cannot use multiple files');
const reader: FileReader = new FileReader();
//esta es la parte que tarda y es justo lo que necesito
reader.onload = (e: any) => {
/* read workbook */
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, {type: 'binary'});
/* grab first sheet */
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
/* save data */
/*esta data es el dato que necesito*/
data = <AOA>(XLSX.utils.sheet_to_json(ws, {header: 2}));
}
if(resolve) {
resolve(data);
} else {
reject('Failure!');
}
});
}
Within my logic I think I need to leave a promise inside another and I have not been able to find information about it. I hope you can help me. Anyway, I appreciate your time.