Hi , in my app i have this method, which opens file, reads it and insert ticket numbers from it into database. So there some promises nested. Is this correct way to solve this? Or there is some more elegant way? I need this method to resolve, only after all promises in it are finished.
Code looks like this:
openFile() {
return this.fileChooser.open().then(uri => {
return this.filePath.resolveNativePath(uri).then((path) => {
console.log(path);
let dirPath = path.substring(0, path.lastIndexOf('/'));
let fileName = path.substring(path.lastIndexOf('/') + 1);
console.log(dirPath, fileName);
if(!fileName.endsWith(".txt")) throw new Error("Nahrát lze pouze textový soubor");
return this.file.readAsText(dirPath, fileName).then((data) => {
this.db.deleteAllTickets().then(() => {
});
let test = data.split('\n');
let i=0;
let sqlArray=[];
test.forEach(ticket => {
sqlArray.push(['INSERT INTO tickets(ticketNumber) VALUES (?)', [ticket.trim()]]);
});
return this.db.insertAllTickets(sqlArray).then((data)=>{
this.ticketsCount=sqlArray.length;
this.testFileWrite("-----Načtení souboru se vstupenkami-----;"+this.generateDateString()+"\n");
});
});
})
});
}