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

155
Views
RxJS: cadena de llamadas con sondeo en el último switchMap

Tengo un ejemplo muy complicado de llamadas RxJS encadenadas con mucho switchMap , etc. Todo está claro para mí hasta que enfrenté un requisito: necesito sondear la última solicitud cada 5 segundos hasta que el objeto de respuesta contenga un valor. Me atasqué un poco... Sé cómo hacerlo si solo tengo encuestas o si solo tengo switchMap simple.

 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)), );

¿Cómo puedo sondear el último switchMap cada 5 segundos hasta que la clave del objeto de respuesta sea, digamos, 'verdadero'? ¿Y volver a intentarlo solo 5 veces?

Lo intenté, pero parece que no funciona:

 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 answers
Answer question

0

Puede crear un solo observable que sondee y emita cuando response.isComplete así:

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

timer() emitirá inmediatamente, luego cada 5 segundos; un máximo de 5 veces gracias a take(5) .

El filter evita las emisiones hasta que se cumpla su condición.

first tomará solo 1 emisión, pero arrojará un error si no se recibe ninguna emisión antes de que se complete la transmisión.

Entonces, en conjunto, podría verse así:

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