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

164
Visualizações
What is a forkJoin alternative that allows parallel requests to complete if one of them fails

I want to run some requests in parallel using forkJoin and combine their results as shown below. However, when one of the requests fails, the rest of the subscriptions are automatically cancelled by the browser. What is a simple alternative to forkJoin that lets me run requests in parallel and if one subscription fails, the rest are allowed to complete?

const posts = this.http.get("https://myApi.com/posts?userId=1");
const albums = this.http.get("https://myApi.com/albums?userId=1");

forkJoin([posts, albums]).subscribe((result) => {
  this.print(result[0], result[1]);
});

print(res1, res2) {
  const message = res1.text + res2.text;
  console.log(message);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can achieve that using forkJoin, however, you have to handle the errors for each sub Observable separately using catchError to prevent canceling the stream if any error occurred.

You can try something like the following:

// import { catchError } from 'rxjs/operators';
// import { forkJoin, of } from 'rxjs';

const posts = this.http
  .get('https://myApi.com/posts?userId=1')
  .pipe(catchError((err) => of(err)));
const albums = this.http
  .get('https://myApi.com/albums?userId=1')
  .pipe(catchError((err) => of(err)));

forkJoin([posts, albums]).subscribe((result) => {
  this.print(result[0], result[1]);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I would make a function like

forkJoinReplaceErrors<T,R>(
  observables: Observable<T>[],
  ifError: ObservableInput<R>
) {
  return forkJoin(
    observables.pipe(
      catchError(() => ifError)
    )
  );
}

and then use that instead of forkJoin. Then it's more reusable and can be unit tested.

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