Come faccio await
su async
funzione chiamata con call
o apply
con Babel
?attendono su una funzione asincrona chiamata a chiamata o applicare con Babel
seguito un esempio, dove getOrders
è un metodo async
di una classe Service
:
class Service() {
async getOrders(arg1, arg2, arg3) {
return await this.anotherService.getOrders(arg1, arg2, arg3);
}
}
let service = new Service();
// ...
// Babel doesn't compile
// let stream = await service.getOrders.call(this, arg1, arg2, arg3);
// producing SyntaxError: Unexpected token for await
let stream = service.getOrders.call(this, arg1, arg2, arg3);
stream.pipe(res); // obviously not working without await in the prev line
UPDATE/SOLUZIONE
Il problema era che let stream = service.getOrders.call(this, arg1, arg2, arg3);
sia in una funzione anonima all'interno di una funzione regolare. Invece di contrassegnare la funzione anonima async
, l'ho fatto per una normale funzione che causa Babel SyntaxError: Unexpected token
.
Grazie a @loganfsmyth per avermi condotto alla soluzione.
stai chiedendo cosa fare quando si desidera chiamare una funzione asincrona al di fuori di un'altra funzione asincrona? Non è chiaro cosa questo abbia a che fare con call/apply/bind, o cosa 'questo' stai passando a' call' nella tua domanda. – loganfsmyth
@loganfsmyth no, sto chiedendo come 'attendere 'su una funzione' async' chiamata con 'call' o' apply'. – krl
Si dovrebbe attendere come si fa normalmente. Aggiornato la mia risposta. – loganfsmyth