jdfiddle: https://jsfiddle.net/2khtof8s/1/
class TestClass {
doSomething(){
return Promise.resolve(this.someFunction('hello'))
}
someFunction( testVar ){
return testVar;
}
}
let instance = new TestClass();
// throws error
Promise.resolve()
.then(instance.doSomething)
.then(console.log)
.catch(console.error);
// works
Promise.resolve()
.then(() => instance.doSomething())
.then(console.log);
// works
Promise.resolve()
.then(function(){return instance.doSomething()})
.then(console.log);
// works
function someFunc(){
return instance.doSomething();
}
Promise.resolve()
.then(someFunc)
.then(console.log);
the first Promise.resolve chain errors out with Uncaught (in promise) TypeError: Cannot read property 'someFunction' of undefined
- we can't seem to understand why, does anyone have any insight into this behaviour? As best we can tell, it seems like there shouldn't be a difference