繁体   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