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

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

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 Relatório

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 Relatório

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