如果在非async函数中使用await,JavaScript引擎会抛出一个SyntaxError,错误信息通常为:“await is only valid in async functions and the top level bodies of modules”。 在CommonJS模块的顶层代码中,由于Node.js默认不支持顶层await(直到Node.js 14及更高版本在某些配置下支持),因此也必须使用async函数来包裹await语...
async function start() { return await getData(); } Copy 2、如果在顶级模块,使用一个立即执行的 async 匿名函数包裹await 语句(async() => { await getData(); })(); Copy 注:方案2 仅在 Node.js 中有效;在Chrome 浏览器上(当前测试v99.0),错误信息会是这样: "await is only valid in async fun...
To fix thesyntaxerror: await is only valid in async functionerror message, you can wrap the await keyword into an async function. Theawaitkeyword can only be used inside an async function. Syntaxerror await is only valid in async function in Node.js If you want to use the await keyword at...
SyntaxError: await is only valid in async function 这个错误的意思是await只能放到async函数内部,言下之意: await必须放到函数里 函数必须有async修饰符 const myFun=async ()=> { return new Promise((resolve, reject)=> { setTimeout(()=> { resolve(1) },1000) }) } const myFun2=async ()=> ...
我在Node 8 上使用Sequelize.js 尝试使用await时出现以下错误。 SyntaxError: await is only valid in async function 代码: async function addEvent(req, callback) { var db = req.app.get('db'); var event = req.body.event db.App.findOne({ ...
var helper = require('./helper.js'); var start = function(a,b){ ... const result = await helper.myfunction('test','test'); } exports.start = start; 我收到一个错误: await is only valid in async function 问题是什么? 原文由 j.doe 发布,翻译遵循 CC BY-SA 4.0 许可协议 javascri...
也要注意 await 必须写在 async 函数里,否则会报错SyntaxError: await is only valid in async functions and the top level bodies of modules。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 // 错误的操作(()=>{await'A';})(); 这样写也是不行的,在 “协程” 一讲中也提过类似的示例,只不过...
openpgp.encrypt(options).then(function(ciphertext) { encrypted = ciphertext.data; // '---BEGIN PGP MESSAGE ... END PGP MESSAGE---' }); SyntaxError: await is only valid in async function at new Script (vm.js:51:7) at createScript (vm.js:136:10) at Object...
eggper1楼
at node:internal/main/run_main_module:17:47 或者 const data = await CSVtoJSON().fromFile('./input.csv'); ^^^ SyntaxError: await is only valid in async functions and the top level bodies of modules at Object.compileFunction (node:vm:352:18) at ...