Falender
Falender

Reputation: 1

Why my Promise Dont do the job?

Im struggling with promise i think i understand the concept but for my project this don't work,

Here a bit of my code :

(I'm coding in TypeScirpt with Angular 2 and Ionic2)

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)
  });
}

Result :

testStatut send a responde before loadStatut and I need to have the opposite .

I try to test with other function and it's work :

ngOnInit() {
  Promise.resolve(this.test1()).then(() => this.test2());
}

test1() {
  console.log("1")
}

test2() {
  console.log("2")
}

Result : Here the code is legit test1 then test2

Upvotes: 0

Views: 56

Answers (1)

Reza
Reza

Reputation: 19843

I change your code as below, try that

ngOnInit() {
  this.loadStatut().then(() => this.testStatut());
}

testStatut() {
  if (this.admin !== undefined) {
    this.navCtrl.push(ConnectPage);
  } else {
    console.log("Undefined")
  }
}

admin;
loadStatut() {
  return this.storage.get('admin').then((val) => {
    this.admin = val;
    console.log(this.admin)
  });
}

Upvotes: 2

Related Questions