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

210
Views
how to check if a method api call is finished and then go to next step in angular

I have two methods in both I am doing api calls. Method one is calling out method 2 and should continue with its process after method 2 api call is finished. I tried to do this with the complete function inside subscription, but I am having the problem that method 2 is contiinuing doing its own work without waiting for method 2 to finish. Can someone tell me where my mistake is?

 getPerson() {
    this.setSelectedFlag() --> Whenever this finishes continue with method
    this.personArray = this.form.controls.person.value;
    this.personArray .forEach((id) => {
      if (!this.personMap.has(id)) {
        this.loading.start();
        this.api.getpersonsData({
          id,
        }).subscribe((response) => {
          this.personMap.set(id, response);
          this.loading.stop();
        });
      }
    });
    this.personMap.forEach((person, id) => {
      if (!this.personArray.includes(id)) {
        this.personen.delete(id);
      }
    });
  }

  setSelectedFlag() {
    this.personArray = this.form.controls.person.value;
    this.personArray.forEach((id) => {
      if (!this.setFlagForPerson.has(id)) {
        this.personArraySaving.ids.push(id);
        this.api.setSelectedForPerson({
          body: this.personArraySaving,
        }).subscribe({
          next: (data) => {
            console.log(data);
          },
          complete: () => {
            return --> I thought this would signal its finish
          },
        });
      }
      });
    
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using promises would allow you to let setSelectedFlag() return a promise. After it returns you can continue with the code in getPerson()

getPerson() {
 this.setSelectedFlag().then(() => {
      // rest of the code.
 })
}


setSelectedFlag(): Promise<void> {
    return new Promise((resolve) => {
            ....
           complete: () => {
             resolve();
           }
    })
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

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!