-2

here is my code :

public data: any;

constructor(private questionsService: QuestionsService) {

 this.questionsService.getAll().pipe(first()).subscribe(data => this.data = data);

 console.log(this.data);

}

How to console.log my api data, outside of subscribe in angular ?

jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
  • 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 Apr 11 '22 at 07:22
  • why do you need this? what are you going to do? – shutsman Apr 11 '22 at 07:49

1 Answers1

0

You can use async in ngOnInit and await firstValueFrom().

  async ngOnInit() {
    this.data = await firstValueFrom(this.questionsService.getAll());
    console.log(this.data);
  }

But this is converting to Promises instead of using Observables as intended.

Chris Hamilton
  • 9,252
  • 1
  • 9
  • 26