I need help with waiting for data to be retrieved before returning it. The code below fetches data from indexedDB and sends it back to a component. I understand that observables or promises can accomplish this, but I am struggling with how to implement them properly. Currently, the return statement executes before the code above completes.
retrieveData() {
let dbReq = indexedDB.open('myChatDb', 1);
let db;
let objectStore;
let results;
let transaction;
let data = new Array();
dbReq.onsuccess = function (event) {
db = (event.target as IDBOpenDBRequest).result;
transaction = db.transaction(["customer-messages"]);
objectStore = transaction.objectStore('customer-messages');
results = objectStore.openCursor();
results.onsuccess = function (event) {
let cursor = event.target.result;
if (cursor) {
data.push(cursor.value);
cursor.continue();
} else {
console.log(data);
console.log(data.length + " array length line 36");
}
}
return data;
}