È o sarà possibile avere un getter classe ES6 restituire un valore da una funzione attesa/asincrona ES2017.(ES6) class as2/aspit getter
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');
Questo è tutto ciò che serve: 'ottenere bar() {return someAsyncOperation(); } ' –
@FelixKling Ho aggiornato il mio post per spero di chiarire la mia domanda. Non sto cercando di restituire una funzione dal getter. Voglio che il valore restituito provenga da un'operazione asincrona. – Enki
Sì, 'return someAsyncOperation();' restituisce la promessa che 'someAsyncOperation' restituisce. Non restituisce una funzione (cosa ti ha fatto pensare che?) –