【发布时间】:2018-02-10 14:05:07
【问题描述】:
我在使用 Node 8.3.0 的 async/await 时遇到了一些静态函数的问题。
MyClass.js
class MyClass {
static async getSmthg() {
return true;
}
}
module.exports = MyClass
index.js
try {
const result = await MyClass.getSmthg();
} catch(e) {}
使用此代码,我在MyClass 上获得了一个SyntaxError: Unexpected token。
这是为什么? await 不能使用静态函数还是我犯了错误?
谢谢
【问题讨论】:
-
你确实导入了 MyClass?
-
您只能在
async函数中使用await。您是否使用函数包装来自index.js的代码? -
忘记将其包装到
async函数中。谢谢大家
标签: javascript node.js async-await