在自定义类文件(ets)中,创建了一个工具类,导出对象时需要调用异步方法,编辑器提示:
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher. "
是需要修改项目配置,还是需要换一种写法?代码示意如下:
// A.ets
class A {
async static create():A{ ....}
}
const A_Instance:A = await A.create()
export default A_Instance
async关键字是用来声明一个函数为异步函数,并使用await关键字等待Promise的解析,需要换种写法,写法如下: