0
this.penService.user(this.phone).then((response) => {
      this.data = response;
      console.log("Data: ", this.data);   // here the response is available   
      return this.data;
    }).catch(err => console.error(err));

    console.log(this.data); // this is where it is undefined or not available

I have the above code which returns the correct response and gives me the value inside the call.

but when I call the same response outside, it is not available.

Any help would be great!

Bob
  • 467
  • 6
  • 25

1 Answers1

0

use async/await

try{

this.data = await this.penService.user(this.phone)

}catch(error){
   console.log(error)
}



console.log(this.data);
underscore
  • 6,495
  • 6
  • 39
  • 78