我想在我的 typescript Nodejs 项目中有顶级的等待。
我的 tsconfig 曾经是这样的:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
我现在将其切换到
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
我还将
"type": "module"
添加到了我的 package.json 中。事实上我现在有能力做顶级的等待但是
commonjs 的方式是如此优雅 - 我可以与 esnext 具有相同的行为,还是在获得顶级等待的同时以其他方式保留它?
以下是 tsconfig.json 文件的示例:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
},
}
我不知道这是否是一个选项,但我认为最简单的方法是将应用程序的入口包装到某种异步的
main()
函数中并执行该函数。在这个函数中,您可以根据需要使用await。