Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Angular call to REST API not waiting for the data to arrived

I have the following code:

export class Whatever implements OnInitn{

    prices;

     ngOnInit(){
       this.CoinPricesService.getPrices().subscribe(
           pricesFromResponse => {
             this.prices = pricesFromResponse;
              console.log('this is what I get from the api: ', this.prices);
           }
       );
       
      console.log('this is my prices class object', this.prices);
     }
}

the output is this:

this is my prices class object undefined

api: this is what I get from the $, $ $

as you can see the first message is showing in second and the second is in the first place with an UNDEFINED, obviously what is happening is that the code is not waiting for the response to come, is being sychronous, WHY ? isn't observable.Subscribe(response ==> this.myVar = response); meant to take care of that ? I can't use the data in other places of my class because it's always undefined when the codes hits there, please help

My Service Looks like this:

@Injectable({
  providedIn: 'root'
})
export class CoinPricesService {

  private apiURl = 'https://api.forprices/blablabla';

  constructor(private http: HttpClient) { }

  getPrices(){
    return this.http.get<any>(this.apiURl);
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you had a function that depended on price, then you could execute the functions within the subscribe. something like this:

    export class Whatever implements OnInitn{
    
        prices;
    
         ngOnInit(){
           this.CoinPricesService.getPrices().subscribe(
               pricesFromResponse => {
                 this.prices = pricesFromResponse;
                  console.log('this is what I get from the api: ', this.prices);
                  someFunction(pricesFromResponse)
               }
           );
           
          
         }
         someFunction(prices){//use price ...} 
    } 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!