Having trouble with promises, I believe I grasp the concept but it's not functioning as expected in my project.
Here is a snippet of my code :
(I am working with TypeScript using Angular 2 and Ionic 2)
ngOnInit() {
Promise.resolve(this.loadStatut()).then(() => this.testStatut());
}
testStatut() {
if (this.admin !== undefined) {
this.navCtrl.push(ConnectPage);
} else {
console.log("Undefined")
}
}
admin;
loadStatut() {
this.storage.get('admin').then((val) => {
this.admin = val;
console.log(this.admin)
});
}
testStatut
sends a response before loadStatut
, whereas I require the opposite order.
I experimented with other functions and they worked as expected :
ngOnInit() {
Promise.resolve(this.test1()).then(() => this.test2());
}
test1() {
console.log("1")
}
test2() {
console.log("2")
}
In this example, test1
precedes test2
execution orderly.