We're a place where coders share, stay up-to-date and grow their careers.
We still need to wrap the method in an arrow function to keep this in the correct context, unfortunately.
this
class myClass { myPrivateVal = "appendedValue"; appendMyPrivateVal(argument) { return argument + ' ' + this.myPrivateVal; } } async function someAsyncMethod() { return new Promise<string>((resolve) => setTimeout(() => resolve("asyncResult"), 1000)); } var myInstance = new myClass(); (async () => { const result = someAsyncMethod().then(myInstance.appendMyPrivateVal); result; // asyncResult undefined const result2 = someAsyncMethod().then((arg) => myInstance.appendMyPrivateVal(arg)); result2; // asyncResult appendedValue })();
I really hate this sometimes.
We still need to wrap the method in an arrow function to keep
this
in the correct context, unfortunately.I really hate this sometimes.