admin管理员组文章数量:1335432
I know of the ES6 await
feature and I’d like to use it in a function I created in a class.
It works well, but when the function is a static
function, it doesn’t. Is there any reason for that? Also, what will be the right way to be able to use await
inside a static
function?
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();
I know of the ES6 await
feature and I’d like to use it in a function I created in a class.
It works well, but when the function is a static
function, it doesn’t. Is there any reason for that? Also, what will be the right way to be able to use await
inside a static
function?
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();
Share
Improve this question
edited Apr 3, 2018 at 0:22
Sebastian Simon
19.5k8 gold badges61 silver badges84 bronze badges
asked Apr 3, 2018 at 0:02
John DavidJohn David
7722 gold badges9 silver badges21 bronze badges
1
- It should work. In what way does it not work? – Get Off My Lawn Commented Apr 3, 2018 at 0:05
2 Answers
Reset to default 4You can use await
just fine in a static function. That is not your issue.
BUT, this
in a static function is MyClass
so this.someMethod()
is looking for another static method, not an instance method and resolveAfter2Seconds()
is an instance method, not a static method so this.resolveAfter2Seconds()
won't find that method because that's like calling MyClass.resolveAfter2Seconds()
which doesn't exist.
If you also make resolveAfter2Seconds()
be static
, then it would probably work because this
inside asyncCall()
is MyClass
so this.resolveAfter2Seconds()
is looking for another static method.
This should work where you make resolveAfter2Seconds
also be static:
class MyClass {
static 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"
}
}
Or, you could reach into the prototype and call it from there because it is actually a static method (doesn't reference this
at all):
static async asyncCall() {
console.log('calling');
var result = await MyClass.prototype.resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}
You're calling await this.resolveAfter2Seconds();
as if the this
was an instantiation of MyClass
, but in the context that your'e calling it, it isn't - resolveAfter2Seconds
is a method on the prototype of MyClass
, it's not a property of the class itself. Call the prototype method instead:
class MyClass {
resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
static async asyncCall() {
console.log('calling');
var result = await this.prototype.resolveAfter2Seconds();
console.log(result);
// expected output: "resolved"
}
}
MyClass.asyncCall();
本文标签: javascript“await thismethod()” doesn’t work in static methodStack Overflow
版权声明:本文标题:javascript - “await this.method();” doesn’t work in static method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742387178a2465266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论