0

this is my code

const openDb = async() => {
  var db: IDBDatabase;
    var request =  await indexedDB.open("adminDB");
     request.onsuccess = (event: any) => {
      db =  event.target.result;
    };
    return db;
}

what i'm trying to do is to await for db = event.target.result; to be done before return db; how can i do that? i know basicly how async/await works for functions but it does not seem to be the same for this situation. any help plz

evals
  • 1,750
  • 2
  • 18
  • 28
  • 1
    You will find it much easier to use a wrapper library that uses promises. You are using `await` but `indexedDB.open()` doesn't even return a promise which makes it pointless to use `async/await` here – charlietfl Jul 18 '20 at 20:47
  • 1
    you can use promise in this case in resolve db inside request.onsuccess, I believe this should do the trick – Abdelrahman Hussien Jul 18 '20 at 20:49
  • Some of the possible wrapper libraries are mentioned in MDN docs. Others are easy to search for https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB – charlietfl Jul 18 '20 at 20:51
  • check out [idb](https://github.com/jakearchibald/idb) – Thomas Jul 18 '20 at 21:12
  • @charlietfl i really prefer to use core indexedDB, bad habit i know but i prefer core things – evals Jul 19 '20 at 09:37
  • @AbdelrahmanHussien i will give it a shot. thank u – evals Jul 19 '20 at 09:37
  • Ok, just going to be more time consuming and tedious using the native API directly when using a well documented lib like dexie.js makes it much easier – charlietfl Jul 19 '20 at 10:47

0 Answers0