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

153
Views
Angular hace una llamada api asíncrona y usa el resultado

Estoy tratando de hacer una llamada a la API

 ngOnInit(){ this.function1() } function1(){ this.userService.getUser() .then((data) => { this.user = data.user this.userM = data.userM // here problem: If I make a console.log(this.userM) it result at the beginning empty if(this.userM.length > 0 ) { console.log("goInside")} else { console.log("doesn't enter") //it enters always there } } }

en mi servicio:

 async getUser(): Promise<any>{ let user: any = [] let userM: any = [] //.... operation to populate return {user, userM} }

Cuando hago una llamada a la API, usaría la matriz this.userM para realizar algunas operaciones. Pero siempre queda vacío al principio, pero ya no entra en si. ¿Como lo puedo hacer?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Utilice rxjs , intente usar un Observable en su lugar:

 import { of } from 'rxjs'; getUsers(): Observable<any> { let user: any = [] let userM: any = [] //.... operation to populate return of({user, userM}); }

Y luego, dentro de su componente:

 ngOnInit(){ this.function1() } function1(){ this.userService.getUser().subscribe((response) => { const { user, userM } = response; if (userM.length) console.log('There is data in it!') else console.log('No data found...'); }); }

Sugiero no usar Promises en un proyecto Angular a menos que sea necesario, tiene a su disposición rxjs junto con toneladas de operadores útiles para manejar llamadas asíncronas/flujos de datos

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!