【发布时间】:2018-09-12 04:59:01
【问题描述】:
我知道 ES6 await 特性,我想在我在类中创建的函数中使用它。
它工作得很好,但是当函数是 static 函数时,它就不行了。有什么理由吗?另外,在static 函数中使用await 的正确方法是什么?
class MyClass {
resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
static async asyncCall() {
console.log('calling');
var result = await this.resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}
}
MyClass.asyncCall();
【问题讨论】:
-
它应该可以工作。它在什么方面不起作用?
标签: javascript static async-await es6-promise es6-class