Title rewritten to: "(ES6) Class (ES2017) Async/Await Getter Method"
P粉564192131
2023-08-22 22:10:47
<p>Is it possible or possible in the future to return a value from an ES6 class's getter via ES2017's await/async functions. </p>
<pre class="brush:php;toolbar:false;">class Foo {
async get bar() {
var result = await someAsyncOperation();
return result;
}
}
function someAsyncOperation() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve('baz');
}, 1000);
});
}
var foo = new Foo();
foo.bar.should.equal('baz');</pre>
<p><br /></p>
Update: As others have pointed out, this doesn't really work. @kuboon provided a nice solution below.
You can do this
This is the same as the code below