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();