How to make the variable come out

Asked

Viewed 40 times

-4

I’m not managing to bring the variable out of the then.

  anggota: any;
  ida: number;
  ionViewWillEnter(){
    this.storage.get('session_storage').then((res)=>{
      this.anggota = res;
      this.username = this.anggota.username;
      this.ida = this.anggota.user_id;
      console.log(this.ida);
    });

    console.log(this.ida);
  }

In the above code the second console.log is not printing this.ida.

  • 2

    Yeah, you’re not gonna make it, this.storage.get() is a Promise, i.e., an asynchronous function. The second log does not yet have the result of the past!

1 answer

-1

try to use async and await

anggota: any;
ida: number;
async ionViewWillEnter() {
  this.anggota = await this.storage.get('session_storage');
  this.username = this.anggota.username;
  this.ida = this.anggota.user_id;
  console.log(this.ida);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.