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

131
Views
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 answers
Answer question

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 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!