I have class in which I'm trying to chain promises like this:
class Parent {
constructor() {
this.driver = []
}
test() {
this.method1()
.then(this.method2)
.then(() => {
console.log('all done', this.driver)
})
.catch(err => {
console.log('Something went wrong', err);
});
}
method1() {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.driver.push('0');
resolve();
},200)
});
}
method2() {
return new Promise((resolve, reject) => {
setTimeout(() => {
this.driver.push('1');
resolve();
},200)
});
}
}
let instance = new Parent();
instance.test();
but method2
is causing error
Uncaught TypeError: Cannot read property 'driver' of undefined