Is it or will it be possible to have an ES6 class getter return a value from an ES2017 await / async function.
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');
get bar(){ return someAsyncOperation(); }
– Pukereturn someAsyncOperation();
returns the promise thatsomeAsyncOperation
returns. It doesn't return a function (what made you think that?) – Pukeasync/await
is just syntactic sugar around promises + generators. It lets you write code that looks synchronous, but it still runs asynchronously. At the top level you still have to deal with the promise. You might be able to doawait foo.bar
, but if not, you have to deal with the promise returned byfoo.bar
directly. – Pukeget async functionName(){}
. I'd like the ability to await inside of a getter (directly) instead of the round-about ways answered here. – Superphosphate