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

1.6K
Visualizações
are lastValueFrom and firstValueFrom equivalent in Angular HTTP?

In angular, we use HttpClient to make HTTP calls which return an observable, if we wanna use promises we can use lastValueFrom/firstValueFrom.

Let's say we have:

async getLast() {
   const get$ = this.http.get(url);
   const res1 = await lastValueFrom(get$);
}
async getFirst() {
   const get$ = this.http.get(url);
   const res2 = await firstValueFrom(get$);
}

are res1 and res2 always equivalent? what is the correct version to use?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In Angular HTTP client, yes, they are. This is why:

observer.next(new HttpResponse({
  body,
  headers,
  status,
  statusText,
  url: url || undefined,
}));
// The full body has been received and delivered, no further events
// are possible. This request is complete.
observer.complete();

After observer.next(...), observer.complete() gets called synchronously. Since this a synchronous call, there is no difference in using lastValueFrom or firstValueFrom.

The only difference here is that firstValueFrom will resolve the Promise once next(...) gets called, while lastValueFrom will resolve the Promise once complete() gets called. Since next and complete get called synchronously, one after another, there is really no much difference here.

However, one thing to note here is: if you're using reportProgress = true, you will want to use lastValueFrom since you'd like to catch the last value emitted from the producer - that is, either the response or the error. You don't want to resolve the Promise with the progress status update. If you do, then you don't want to use Promises at all.

over 4 years ago · Santiago Trujillo Relatório

0

In the basic usage of HttpClient.get(url) - without any additional options like observe: events or reportProgress: true - both versions are equivalent and you can use whichever you like. The observable will emit only once or fail, therefore both lastValueFrom and firstValueFrom will behave the same.

over 4 years ago · Santiago Trujillo 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