Within my React TypeScript App, I utilize IndexedDB for data storage. To work with IndexedDB, I have a dedicated class called DB. One of the methods in this class is used to retrieve all data.
public getAll(){
const promise = new Promise((resolve,reject)=>{
const req = this.openDB();
req.addEventListener("error", (e) => {
console.log("error ", e);
reject ("error");
});
req.addEventListener("success", () => {
const db = req.result;
const tx = db.transaction("tasks", "readonly");
tx.addEventListener("complete", () => {
console.log("transaction complete");
db.close();
});
const readObj = tx.objectStore("tasks");
const data = readObj.getAll();
data.addEventListener("success",(e)=>{
console.log(data);
console.log(e);
resolve (data.result);
});
});
});
return promise;
}
In one of my component's event handlers:
const db = new DB();
const dataPro = db.getAll();
dataPro.then((resData)=>{
console.log(resData); // Data successfully retrieved
}
Although the getAll function runs smoothly without the use of async-await, I wonder if it's truly necessary to include async-await in this function. Are there specific scenarios where async-await is required?
I have tested the getAll function without async-await and it still functions properly for me.