I am writing a Chrome Extension and have need to store data. There are various options for this, including chrome.storage and indexeddb but all appear to involve async operations.
The first thing my script needs to do is to read a value from the store to decide what to do next – so I need to wait for it to return with a result, BUT it seems in javascript I cannot use await except within an async function.
The only solution I can see is to put my entire top level function into a new async function that I can then call, just so that I can await the call to the data store, which coming from a C# background where calls to SQL for example are typically synchronous, seems odd.
(function() {
import { openDB } from 'idb';
async function createStoreInDB() {
const dbPromise = openDB('example-database', 1, {
upgrade(db) {
// Creates an object store:
db.createObjectStore('storeName', options);
}
});
await dbPromise; // Wait for dbPromise to resolve before continuing
}
async function main() {
await createStoreInDB();
}
main(); // Call main function to start the application
}
Can anyone clarify if this is the correct way to approach it, or is there another way.