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

162
Views
How to call another service/api only if the first one has successfully executed?

I am very new to angular and typescript. I have been trying this.

doSomething() {
    return this.http.post(this.path + "dosomething", data).toPromise();
}

now in .ts you are awaiting it

await servicesCalls.doSomething();

Upon its success implementation I need to do the same with all another maps from service.

But first I want to make sure it has posted the data.

await serviceCalls.doAnotherThing();

The api is returning IHTTPACTIONRESULT i.e. Ok().

Can anyone help me with that? Thanks in advance!

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

0

There is no need to use promises. HTTP methods in Angular return an Observable. You can subscribe to that observable, an when that observable emits some data then in the subscribe method you can call another service.

Something like this::

doSomething() {
    return this.http.post(this.path + "dosomething", data);
}

Now there are various different ways to do this:

Method one: Normal way. Call another service in the subscription of the first one.

Component:

public callService() {
    doSomething.subscribe(data => {
        serviceCalls.doAnotherThing();
    },
    err => {
        // do something
    })
}

Subscription will only be called when doSomething function( that is your HTTP request returns some value).

Method 2: You can use rxjs operators - switchMap like this:

doSomething()
    .pipe(
        //Use switchMap to call another API(s)
        switchMap((dataFromServiceOne) => {
            serviceCalls.doAnotherThing();
        })
    ).subscribe((data) => {
        // do something with response
        console.log(data);
    });

For more details about rxjs operators:

  1. switchMap
  2. Observables
about 4 years ago · Juan Pablo Isaza Report

0

Use switchMap

doSomething() {
    return this.http.post(this.path + "dosomething", data);
}


doAnotherthing() {
    return this.http.post(this.path + "dosomething", data);
}

doSomething().pipe(
  tap(doSomethingResponse => this.performSomeOperation(doSomethingResponse)),
  switchMap(_ => this.doAnotherthing())
).subscribe(doAnotherthingResponse => console. log(doAnotherthingResponse));
about 4 years ago · Juan Pablo Isaza Report

0

You could simply store this in a variable like this,

const promise = servicesCalls.doSomething();

and simply call the next one like this,

promise.then(async () => await serviceCalls.doAnotherThing()).catch(e => console.error(e));

The next API call will only be executed when the previous promise resolves.

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!