(ES6)类(ES7)async / await getter [英] (ES6) class (ES7) async / await getter
本文介绍了(ES6)类(ES7)async / await getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可能有一个ES6类getter
从一个ES7等待/异步函数返回一个值。
Is it or will it be possible to have an ES6 class getter return a value from an ES7 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');
推荐答案
您只能等待
promises,而 async
函数将自动返回承诺。
当然,getter可以产生这样的承诺,还有与正常值无差异。
You can only await
promises, and async
functions will return promises themselves.
Of course a getter can yield such a promise just as well, there's no difference from a normal value.
这篇关于(ES6)类(ES7)async / await getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文