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

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我不明白为什么当我在 async await 函数中工作时会抛出该错误...不确定是否有所不同但我在 chrome 扩展的内容脚本中执行此代码

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:您可能必须使用匿名电话 Function,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - SyntaxError: await is only valid in async functions and the top level bodies of modules await 仅在异步函数和循环中模块的顶层主体中有效 - await is only valid in async functions and the top level bodies of modules in loop await 仅在异步函数和模块的顶级主体中有效 - await is only valid in async functions and the top level bodies of modules 全局调用异步 function 时出错:“等待仅在异步函数和模块的顶层主体中有效”? - Error in global call to async function: "await is only valid in async functions and the top level bodies of modules"? ASYNC / AWAIT SyntaxError: await 仅在异步函数和模块的顶层主体中有效 - ASYNC / AWAIT SyntaxError: await is only valid in async functions and the top level bodies of modules await 仅在异步函数和模块的顶层主体中有效 javascript 表示错误 - await is only valid in async functions and the top level bodies of modules javascript express error javascript await function in script 返回 Uncaught SyntaxError: await is only valid in async functions and the top level body of modules - javascript await function in script returns Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules 打开 AI 示例代码不起作用“等待仅在异步函数和模块的顶级主体中有效” - Open AI example code not working "Await is only valid in async functions and the top level bodies of modules" SyntaxError: await 仅在异步函数和模块的顶层主体中有效。 不和谐,js - SyntaxError: await is only valid in async functions and the top level bodies of modules. Discord,js async/await 模块语法错误的顶级主体 - Top level bodies of modules syntax error with async/await
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM