-1
gametimes:any;

ngOnInit(): void {
this.gamewinService.get_current_gameid_time().subscribe(response =>{
      this.gametimes = response;  
      console.log(response);  
});

console.log(this.gametimes) //Undefiend
}

I am getting response in 'this.gametimes' but not whenever I am trying to getting this outside the response function its showing undefined. So, How to access response outside the function?

eko
  • 39,722
  • 10
  • 72
  • 98
  • 1
    Does this answer your question? [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jonrsharpe Jun 01 '21 at 08:54

1 Answers1

0

Assuming that get_current_gameid_time returns an observable. You can use first operators from RxJs :

import { first } from 'rxjs/operators';

// [...]

gametimes:any;

async ngOnInit(): Promise<void> {
  this.gametimes = await this.gamewinService.get_current_gameid_time().pipe(first()).toPromise();
  console.log(this.gametimes) // should not be undefined
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
undefined
  • 38
  • 6