Pete Pete - 1 month ago 5
TypeScript Question

Javascript/Typescript call external function from within function

I have:

connect(id: string) {
console.log(id);
this.connection = this.api.connect(id);
this.connection.on('open', function () {
this.testConnect();
});
}

testConnect(){

}


I want to be able to call the testConnect function from within the this.connection.on

Answer

Use an arrow function:

this.connection.on('open', () => {
  this.testConnect();
});
Comments