• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

312
Vistas
How to call a function after previous function is complete in angular

I have 2 functions and I would like to call one after the other

mainFunction(){
this.service1.abc();
this.xyz();
}

this.service1.abc is making an API call to get and setting some data in local storage and based on that this.xyz is using that localStorage value. current behavior : this.xyz() get called before the completion of this.service1.abc() and so on localstorage is not getting set.

API Service function doesn't returns any observable,

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

since the API call function is asyncronous you can't just call it in certain order and expect it to be executed in that order.

if your API Service function returns a promise you can do something like this:

   async mainFunction() {
    await this.service1.abc();
    this.xyz();
    }

which will make the function to wait till your async call has finished then move on to execute the XYZ function.

if your API Service function returns an observable, then your can just subscribe to it and trigger your xyz function like that:

mainFunction(){
this.service1.abc().subscribe(res => {
   this.xyz();
})

}
about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda