Martin Pascual
Martin Pascual

Reputation: 35

Ionic Wait For Function To Finish Before Running Second

I have this function in my Ionic App:

clearTransactions() {
    this.clearIndex = this.clearIndex + 1;
    if (this.clearIndex < 3) {
      console.log("click " + (3 - this.clearIndex) + " more times");
    } else {
      this.storage.clearItems()
      this.storage.getItems().then(
        items => {
          this.items = items
          console.log("test", this.items);
        }
      )
      this.clearIndex = 0;
    }
  }

For some reason, the this.storage.getItems() runs even before the this.storage.clearItems() finish. Can anyone suggest a solution for this problem? Thanks!

Upvotes: -1

Views: 309

Answers (1)

user6355773
user6355773

Reputation:

clearItems returns a promise and you have run the rest of the code when it's finished like this:

   this.storage.clearItems().then(()=>{
      // do things after clear
    })

Upvotes: 1

Related Questions