I have a sequence of code that is at the top level of my script:
await tmrs=chrome.storage.local.get("tmrs"); console.log(tmrs); await data=chrome.storage.local.get("Auto_Select_051969"); console.log(data); if (data==null) { store_data(); await data=chrome.storage.local.get("Auto_Select_051969"); } However I get the error:
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules This is in a service_worker script. Is there some kind of restriction? How do I fix this? TIA.
I found this: Getting this error "await is only valid in async functions and the top level bodies of modules" in chrome extension with async await but I'm not sure this applies or if the anonymous function is really the solution.
Updated Code:
(async () => { let tmrs=await chrome.storage.local.get("tmrs"); })(); console.log(tmrs);
awaitkeyword should come immediately after the equals sign, e.g.tmrs=await chrome.storage.local.get("tmrs");