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

234
Visualizações
.toPromise() and lastValueFrom() in rxjs

I have this observable

createMyRecord(): Observable<myRecord> {
        return of(TEMPLATE_DB).pipe(
            mergeMap((template) => doTask(template)),
            mergeMap(() => EMPTY)
        );
    }

I call it with

await createMyRecord().toPromise();

.toPromise() is deprecated, so I would like to change the call with lastValueFrom():

await lastValueFrom(createMyRecord());

but I receive the following error:

EmptyError
no elements in sequence

UPDATE: for now, resolved with:

 await lastValueFrom(createMyRecord()).catch((err) => {
            if (err instanceof EmptyError) {
                log.info(OK");
            }
        });

but is there a better solution?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Is there a better solution?

Yes and no.

In your case mergeMap(_ => EMPTY) will ensure that your observable completes without emitting a value. Promises resolve to a value or they error. So the only thing to do here that meets the spec is to throw an error.

A work-around

You can sidestep this by emitting something. For example, here I emit null after the source completes:

createMyRecord(): Observable<myRecord> {
  return of(TEMPLATE_DB).pipe(
    mergeMap((template) => doTask(template)),
    mergeMap(() => EMPTY),
    s => concat(s, of(null as myRecord))
  );
}

Now your promise will resolve with a null once the observable completes successfully.

Something idiomatic

Rather than changing your code, you can change how you call it. This way you don't need to worry about how Observables and Promises interact.

Instead of await lastValueFrom(createMyRecord()); write:

createMyRecord().subscribe();
about 4 years ago · Juan Pablo Isaza Relatório

0

lastValueFrom now takes a configuration parameter as its second parameter, and you can specify a default value that will be emitted if the observable is empty:

await rxjs.lastValueFrom(observableThing, {defaultValue: "oh no - empty!"})
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