简体   繁体   中英

Getting this error "await is only valid in async functions and the top level bodies of modules" in chrome extension with async await

I can't understand why this is throwing that error when I am working inside async await functions... Not sure if makes a difference but I am executing this code inside the content script of a chrome extension

Thanks in advance!

const getFromStorage = async function(key) {
return new Promise((resolve, reject) => {
    chrome.storage.sync.get(key, resolve);
})
    .then(result => {
        if (key == null) return result;
        else return result[key];
    });
}

let updateArray = await getFromStorage("toSaveArray");

Top level await is not yet supported

You probably have to use an an Anonymous Function, for example:

(async () => {
  let updateArray = await getFromStorage("toSaveArray");
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM