I'd like to reference a class property in my second promise. However, in the class function pTwo, 'this' is undefined. I understand that I'm in the promise scope, how might I access the PromiseChain instance scope?
Using ES6 and native promises.
class PromiseChain {
constructor(){
this.food = 'Pasta';
this.type = 'Italian';
}
pOne() {
console.log('pOne');
return Promise.resolve();
}
pTwo() {
console.log('pTwo');
try {
console.log(this.food);
} catch (e) {
// can't read 'food' of undefined!
console.log(e);
}
return Promise.reject()
}
work() {
console.log('Get to work!');
this.pOne().then(this.pTwo).catch((error) => {
console.log(error);
})
}
}
new PromiseChain().work();