在es2017中,当从asynchronous方法访问时,“this”是未定义的

如何从asynchronous方法调用时引用类实例。

class Senders { callSendAPI() { //Some code here } sendText() { this.callSendAPI(); } async sendCards() { var dbpromise = await db.call(); console.log('this= ', this); this.callSendAPI(); } } export {Senders}; 

这=未定义

问题是无论你正在使用的任何译码器,如果你正在使用一个,或者被调用的函数的上下文。 我在NodeJS v7.x中运行了下面的代码片段,它工作的很好,显示了这个值是发件人的类实例。

 class Senders { async sendCards() { console.log('this= ', this); } } new Senders().sendCards();