如何使用 Babel
在使用 call
或 apply
调用的 async
函数上进行 await
?
以下是一个示例,其中 getOrders
是 Service
类的一个 async
方法:
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
this
是什么。 - loganfsmythcall
或apply
调用的async
函数上进行await
。 - krlcall
不是一个async
函数(尽管在这种情况下它调用了一个async
函数),所以在这里使用await
是无效的,至少目前在babel
中是这样。 - krlawait
只是等待一个 Promise。例如:var streamPromise = service.getOrders(); var stream = await streamPromise;
。你使用call
只是改变了调用getOrders
的方式,它并不影响await
的工作原理。如果你有一个具体的示例无法正常工作,请将其添加到你的问题中。 - loganfsmyth