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

157
Visualizações
RxJS: chain of calls with polling in the last switchMap

I have a very complicated example of chained RxJS calls with a lot of switchMap and etc. Everything is clear for me until I faced one requirement: I need to poll last request every 5 sec until response object contains a value. I stuck a bit... I know how to make it if I have only polling or if I have only plain switchMap.

this.articleService.create(data).pipe(
  switchMap((article: ArticleCreateModel) => this.articleService.getArticle(article.id)),
  switchMap((article: ArticleCreateModel) =>
    this.articleService.update({
      ...article,
      timestamp: this.someFakeService.getData(),
    }),
  ),
  // somehow I need to poll next createAsyncCommentsSection every 5 sec until response object key is let's say 'true'
  switchMap((article: ArticleCreateModel) => this.articleService.createAsyncCommentsSection(article.id)),
);

How can I poll last switchMap every 5 sec until response object key is let's say 'true'. And retry it only 5 times?

I tried so, but it looks like it doesn't work:

  switchMap((article: ArticleCreateModel) =>
    this.articleService.createAsyncCommentsSection(article.id).pipe(
      delay(5000),
      filter((article: ArticleCreateModel) => !!article.hasSyncedWithOtherAPI),
      take(5),
    ),
  ),
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create a single observable that polls and emits when response.isComplete like this:

timer(0, 5000).pipe(
    take(5),
    switchMap(() => this.articleService.createAsyncCommentsSection(articleId)),
    filter(response => response.isComplete),
    first()
);

timer() will emit immediately, then every 5 seconds; a maximum of 5 times thanks to take(5).

filter prevents emissions until your condition is met.

first will take only 1 emission, but will throw an error if no emission is received before the stream is completed.

So altogether, could look like this:

this.articleService.create(data).pipe(
  switchMap((article: ArticleCreateModel) => this.articleService.getArticle(article.id)),
  switchMap((article: ArticleCreateModel) =>
    this.articleService.update({
      ...article,
      timestamp: this.someFakeService.getData(),
    }),
  ),
  switchMap((article: ArticleCreateModel) => timer(0, 5000).pipe(
    take(5),
    switchMap(() => this.articleService.createAsyncCommentsSection(article.id)),
    filter(response => response.isComplete),
    first()
  )
);
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