Chaining Promises in Ionic 3

Hi everyone,

I have a bit of trouble getting my head around this issue. So in my component.ts I want to check everything in the database once the device is ready. I have managed to do a part of it but I have a condition in which if

  1. hasSavings() == true and hasPin() == true, my root page becomes the PinCodePage.
  2. hasSavings() == true and hasPin() == false, my root page becomes the SavingsPage
  3. hasSavings() == false, automatically the root page becomes the TutorialPage

However, I cannot seem to figure out how to do this.

This is my code:

this.dp.createDatabase().then(data => {
        if(data) {
          return this.dp.hasSavings();
        }else {
          return false;
        }
      }).then(data => {
        if(data) {
          this.rootPage = SavingsPage;
        }else {
          this.rootPage = TutorialPage;
        }

        return this.dp.hasCategory()

      }).then(data => {
        if(data) {
          console.log("Tables already populated.");
          return false;
        }else {
          return this.dp.populateTables();
        }

      }).then(data => {
        console.log("Tables successfully populated!");

      }, err => {

          console.log("Nope");

        })
       
      });

Kinda confused on how to use promises. If I also made some mistakes in implementing them please do enlighten me.

Thank you!

HasSavings() hasCategory() are promises?

Should be

.catch(err=>{})

And, if these are promises you need to test all, then you can wrap them in a Promise.all([…promises go here as array]), and then test the result in the resuting array. After which you then select the page to go to. That would make more sense then setting the variable for follow-up page somewhere in between.