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

330
Views
Angular TypeScript: waiting for a void method to run before another method

I have a method in an Angular component that pulls data via HttpClient subscription, and assigns it to an attributes this.allData, then instantiates an empty dictionary of parameters based on this, to pass to a second function:

export class TestComponent implements OnInit {

  allData: object[] = []
  activeData: object = {}

  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    this.getData()
    this.makeRequestBasedOnData()
  }

  getData() {
    this.http.get(this.url).subscribe(res => {
      for (let datum of res["data"]) {
        this.allData.push({
          "key": Object.keys(datum)[0],
          "values": Object.values(datum)[0]
        })
        this.activeData[Object.keys(datum)[0]] = ""
      }
    })
  }

  makeRequestBasedOnData() {
    let testParams = this.activeData
    console.log(testParam)
  }

}

I need these steps to happen sequentially. At the moment, logging the testParams in makeRequestBasedOnData() simply shows an empty object {}. When I try to return arbitrarily in the first method, I get a TypeScript error that you cannot assign a promise to type void.

How do I enforce synchronicity here even though neither method actually returns anything?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can return Observable from getData method and proceed with any other methods within subscribe:

ngOnInit(): void {
  this.getData().subscribe(() => this.makeRequestBasedOnData());
}

getData() {
  return this.http.get(this.url).pipe(
    tap(res => {
      // update allData
    })
  );
}

where:

  • pipe method allows us to provide any kind of transformation with the data returned from http.get(...) call.

  • tap is a rxjs operator for side-effects, meaning that we can do everything we want with the response without modifying Observable flow.

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!