Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda