簡體   English   中英

使用 async await 在 chrome 擴展中出現此錯誤“await 僅在異步函數和模塊的頂層主體中有效”

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

我不明白為什么當我在 async await 函數中工作時會拋出該錯誤...不確定是否有所不同但我在 chrome 擴展的內容腳本中執行此代碼

提前致謝!

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");

尚不支持頂級等待

您可能必須使用匿名電話 Function,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM