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

228
Visualizações
When Response.text() promise will reject?

I see that on MDN/Response/text docs show the example of using .text() only with then

response.text().then(function (text) {
  // do something with the text response
});

It returns a promise that resolves with a String.

Because of lint rules, I need to put

// eslint-disable-next-line @typescript-eslint/no-floating-promises
res.text().then(async (t) => {

Is there a use case when I need to catch a rejected promise from Response.text()? Maybe some examples?

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

0

It can fail/reject if the response was already consumed/read, so i.e if something already called .text/.json etc on it.

Looking at the polyfill implementation (https://github.com/github/fetch/blob/d1d09fb8039b4b8c7f2f5d6c844ea72d8a3cefe6/fetch.js#L301 ) I don't see other likely cases though.


Example:

response.text()
  .then(t1 => {
    console.log({ t1 }); // after calling text() we can see the result here
    return response; // but we decided to return the response to the next handler
  })
  .then(res =>res.text()) // here we try to read text() again
  .then(t2 => console.log({ t2 })) // and expecting text to be logged here
  .catch(er => console.log({ er })); // but the text() promise rejects with 
  // TypeError: Failed to execute 'text' on 'Response': body stream already read
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