I'm having trouble figuring out when do I need to bind the instance to a function.
For example, say I'm using a service function that returns a Promise<void>
and i dont want to use an anonymous function in my then-chain, i'd like to do something like this:
this.someService.somePromiseMethod().then(this.doSomething)`
the doSomething
:
public doSomething(){
this.a = 2
}
But this fails since it does not recognize the a
of this (undefined
)
While using : this.someService.somePromiseMethod().then(this.doSomething.bind(this)
works
WHY? The call to the service is done within the instance, why do I still need to bind this instance to the function call?
Thanks in advance for any clarifications!