I have a function that retrieves data from an indexedDB:
function dbGetProdData() {
return new Promise((resolve)=>{
if (!indexedDBAvailable) {
getDataByFetch();
createSizeFilterOptions();
resolve(false);
}
tprodArr=[];
tcurrentProdArr=[];
console.log(`gettingData: `,currentProdArr.length); //<==this is firing well after the .then() when the function is called.
let requestNew = indexedDB.open(dbName, dbVersion);
requestNew.onupgradeneeded = (event) => {
db = event.target.result;
const objectStore = db.createObjectStore("currentCollection", { keyPath: "id" });
};
requestNew.onsuccess= (event) => {
console.log(`db success`) //this is firing well after the .then()
db=event.target.result;
currentCollectionStore = db.transaction("currentCollection", "readwrite").objectStore("currentCollection");
currentCollectionStore.get(1).onsuccess = async (event) => {
console.log(`retrieving: `,prodArr.length);
if (event.target.result.prodArr.length>0) {
tprodArr=event.target.result.prodArr;
}
if (event.target.result.currentProdArr.length>0) {
tcurrentProdArr=event.target.result.currentProdArr;
}
console.log(`is valid: `,pagesInCollection,tprodArr.length/paginationIndex)
if (+pagesInCollection-1>=+tprodArr.length/paginationIndex) {
await getDataByFetch();
} else {
console.log(`got current data`); //this is firing well after the .then()
prodArr=tprodArr;
currentProdArr=tcurrentProdArr;
console.log(`prodArr: `,tprodArr.length)
tprodArr=null;
tcurrentProdArr=null;
}
};
}
requestNew.oncomplete=function() {
resolve(true);
}
});
}
The issue is I need to wait for the results before the rest of the javascript continues.
I'm calling it like this:
dbGetProdData().then(function() {createSizeFilterOptions()});
How do I call it so that execution pauses until the indexedDB functions are complete? I can't use await because of where I'm calling this. I'm not in an async function.