javascript - 异步/等待语法错误 : await is only valid in async functions and the top level bodies of modules

标签 javascript node.js asynchronous async-await

我正在做一些关于使用 csvtojson node module 将 csv 文件读取为 json 格式的非常简单的测试。 ,我使用下面的代码作为模板

a,b,c
1,2,3
4,5,6
*/
const csvFilePath='<path to csv file>'
const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
    console.log(jsonObj);
    /**
     * [
     *  {a:"1", b:"2", c:"3"},
     *  {a:"4", b:"5". c:"6"}
     * ]
     */ 
})
 
// Async / await usage
const jsonArray=await csv().fromFile(csvFilePath);
我主要关注的是

// Async / await usage


const jsonArray=await csv().fromFile(csvFilePath);


代码部分。所以这是我的代码
// const JSONtoCSV = require("json2csv")
// const FileSystem = require("fs")

async function test()
{
    const data = await CSVtoJSON().fromFile('./input.csv')
    return data
}

let temp = await test()

console.log(temp)
无论我尝试过哪种方式,我总是会收到以下错误
let temp = await test()
           ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    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 wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47
如果我像这样将代码切换为所有顶级
const CSVtoJSON = require("csvtojson")
// const JSONtoCSV = require("json2csv")
// const FileSystem = require("fs")

const data = await CSVtoJSON().fromFile('./input.csv')

console.log(data)
我不明白为什么这不起作用。
编辑:我按照@tasobu 的说明进行了更改。现在我得到的只是一个返回的 promise
const data = (async () => {
    return await CSVtoJSON().fromFile('./input.csv')
})

console.log(data)
Debugger attached.
Promise { <pending> }
Waiting for the debugger to disconnect...

最佳答案

您可以在此处使用 IIFE(了解更多信息 here)

(async function main () {
    // You can use await inside this function block
})();

关于javascript - 异步/等待语法错误 : await is only valid in async functions and the top level bodies of modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69089471/

相关文章:

javascript - 如何修改这个程序,我想要下一页链接

javascript - 在表格单元格中创建按钮以更改其所在行的颜色

javascript - AngularJS 中的 HTTP POST 请求返回 403

javascript - Node.js 中 sqlite 数据库的异步 HTTP 请求

node.js - Vertx、Node 或 Play 用于异步调用多个 Web 服务

python异步和httpx

javascript - 从下拉列表中选择时,Angular JS ng-model 变为 null

javascript - $routeParams 中的参数未定义,但 $routeParams 未定义

javascript - "loose": true is not fixing Support for the experimental syntax 'classProperties' isn't currently enabled

javascript - 带有 NODESSPI 的企业环境中的 Node