I'm learning JS classes and have run into an issue. I cannot figure out how to self-reference my class variables (set in the constructor) when I lose this context, like after a Promise.
For example:
class myClass{
constructor(name) {
this.name = name;
doSomething().then(doSomethingElse)
}
doSomething(){
return new Promise((resolve, reject)=>{
resolve("myAsyncName")
})
}
doSomethingElse(value){
this.name = value;
}
}
In the doSomethingElse function, I'll get an error that this is undefined or cannot set name on it. I have tried using setting self = this in the constructor which works but breaks if I use the class more than once. I simply need to be able to reference variables set in my constructor. I've tried searching many posts and reading a plethora of articles on how to use this, and bind, but I cannot find the pattern I need to use in this situation.