我如何用TypeScript运行一个顶级的异步等待函数?

问题描述 投票:0回答:1

我的文件是

import * as fs from 'fs'

async function loadModels() {
    console.log('here i am!')
    const modelFiles = fs.readFileSync(__dirname + '/models')
    console.log(modelFiles)
}


(async () => {
    loadModels()
})()

而在 package.json,我有。

"fixtures": "tsc fixtures/index"

所以当我运行 yarn fixtures我明白了

yarn fixtures
yarn run v1.22.4
$ tsc fixtures/index
✨  Done in 8.78s.

为什么我的... loadModels 运行?

javascript node.js typescript async-await
1个回答
4
投票

tsc 只是编译你的TypeScript文件,而不是执行它。

来执行你的TypeScript文件,你可以使用 ts-node 包裹

然后在你 package.json:

"fixtures": "ts-node fixtures/index"
© www.soinside.com 2019 - 2024. All rights reserved.