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

132
Vistas
How to make subscribe method to run first & then proceed further

I am new to Angular and struck with subscribe method. I need to trigger subscribe first in which initialization of property is done. Please help me with this. Here is my code..

json.service.ts

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

constructor(private http: HttpClient) {}
//This fetch the json present in assets folder
url: string = `${window.location.origin}/assets/urls-list.json`;

getRestJson() {
this.http.get(url);
}

url.setting.ts

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

fetchedJson: Array<any>;
constructor(private service: JsonService) {
 this.fetchingJsonFromService();
}

fetchingJsonFromService() {
 this.service.getRestJson().subscribe(
 response => {
 this.fetchedJson = response;
});
}

private static getValue(key: string) {
// use the key do some stuff with "this.fetchedJson" 
and return only that value back to getServiceUrl()

return something;
}

public static getServiceUrl(key:string) {
  const result = UrlService.getValue(key);
  return result;
}

}
  • From debugging this code realized that "getValue()" is called first and the "this.fetchedJson" is undefined. Then the call goes to "fetchingJsonFromService()" where fetchedJson gets initialized which is too late. Kindly help me to solve this issue. It would be of great help.

  • "getServiceUrl(key)" - is called by multiple services throughout the application

Thanks a lot!! 😊

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

0

As getRestJson() is async you'll need to execute getValue() when it's response has arrived, which You can simply do by calling getValue() inside your fethcingJsomFromService()

fetchingJsonFromService() {
 this.service.getRestJson().subscribe(
 response => {
 this.fetchedJson = response;
 this.getValue();
});
}

private getValue() {
// do some stuff with "this.fetchedJson" and return only that value to other method
}
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