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

198
Visualizações
How to run subject after error happens in rxjs?

Why subject.next(2); not works? I know it may happens because the signal stream got error. But I add catchError and I return a EMPTY in the pipeline and it make it complete.

So seems catchError not helping me either.

How to make subject.next(2) works?

My code is calling to doWork function. inside I invoke work function. if it's one I simulate an error at the first time I call the function, after that should not have error.

Then I expose the subject and the subject-pipeline to do something else after the pipeline is invoked. I use share for that (to not duplicate my flow) and it should be happens only once.

stackblitz.com

import { EMPTY, of, Subject, throwError } from 'rxjs';
import { exhaustMap, share, take, tap } from 'rxjs/operators';

console.clear();

const doWork = () => {
  const subject = new Subject();

  const work = (n) => {
    console.log('in work fn', n);
    if (n === 1) {
      throw new Error('bla');
    }
    return of([]);
  };

  const pipe = subject.pipe(
    exhaustMap((n) => work(n)),
    share()
  );

  pipe.subscribe({
    next: () => console.log('in subjectPipe next'),
    error: () => console.log('in subjectPipe error'),
    complete: () => console.log('in subjectPipe complete'),
  });

  return { subject, pipe };
};

const { subject, pipe } = doWork();

pipe
  .pipe(
    take(1),
    tap(() => console.log('continue the pipe...'))
  )
  .subscribe({
    next: () => console.log('in subject next'),
    error: () => console.log('in subject error'),
    complete: () => console.log('in subject complete'),
  });

subject.next(1);
subject.next(2);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are few things here:

  1. A subject which is a Subject
  2. A pipe in the doWork function. Such pipe is actually a different object from subject. It is an Observable returned by the pipe function invoked on subject. subject is the source of pipe, but it is not pipe
  3. pipe errors when processing the first notification, given the work function logic
  4. A subscription to pipe

When an Observable errors, than it implicitly completes, so it will not notify any other value.

pipe errors immediately and therefore it will not notify any more, even if its source, which is subject, continues to notify.

Therefore you can do subject.next(2) but this notification will not be processed by pipe since pipe has already errored.

There is one more thing. There is a take(1) in the bottom pipe. This means that we take the first notification and then we complete.

Therefore, even if you remove the logic the throws the error, you will not see a second continue the pipe... message printed on the console since the Observable completes after the first notification.

about 4 years ago · Juan Pablo Isaza Relatório

0

I will extend on @Picci's answer:

To combat the fact, that when an emitter (Subject in this case) errors it cannot emit anymore, we can emit an Error instance instead. Handling such error will require more non-standard code, but it is definitely an easy to read solution to the given problem.

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