Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
How to get response from one promise in two different methods in AngularJS

I need to do some changes to a legacy AngularJS application.

I have two methods in a controller:

const params: any = {id: 1};
public getGridData(): void {
  this.dataService.getPanelData(params).then((response: any) => {
     const dataToBeUsedInGrid = response.data.locationDetails;
     // extra code
  }
}

public getMapData() : void {
  this.dataService.getPanelData(params).then((response: any) => {
     const dataToBeUsedInMap = response.data.locationDetails.Coordinates;
     // extra code
  }
}

And this is the code in dataService which is just making an api call:

public getPanelData(params: any) {
        return this.$http.post(this.dataConfigService.url, params);
    }

Now this code is making two api calls. I just make to make one api call to server and use the response in different places as the response is same in both the calls. Both the getGridData and getMapData methods are called together asynchronously upon a record selection, and are responsible for displaying different data on the same page.

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

0

Conceptually a simple solution would be to refactor the code (functions) that call getGridData and getMapData into functions that are passed the data they need to update the page as an argument. Call the modified functions handleGridData and handleMapData for now.

Now refactor record selection to call getPanelData, then handleGridData, and then handleMapData in the asynchronous coding style of your choice. If you use async functions and the await operator you can probably in-line the code for the proposed handler functions, as well as handling errors gracefully.

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda