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

208
Visualizações
Why am I getting a pending promise even in .then block?
import fetch from "node-fetch"
const url = "https://jsonplaceholder.typicode.com/posts/1"
const getData = fetch(url)
getData.then((data)=>{console.log(data.json())})

Making a simple GET request using node-fetch. I am still getting a pending promise here while I am using .then block.

My thesis is that after the promise has resolved that is the data is returned from the server which will send the funciton definition inside the .then block to the microtask queue from where it will be taken to the call stack by the event loop.

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

0

The process of fetching data is split into two parts.

The first part waits for the meta-information (status code and other headers). The second part for fetching the contents (body) of the response.

import fetch from "node-fetch"
const url = "https://jsonplaceholder.typicode.com/posts/1"
const getData = fetch(url)
getData.then((response)=>{
   // here the meta information of the response is received,
   // but the body might still be transimmited.

   // returns the promise that waits for the body to be received as well
   return data.json()
})
.then((data) => {
  // here the body is received and parsed
  console.log(data)
})

This is especially helpful if the files you request are large or take long to download for other reasons. And you get the information of the files early: like name, size (if the server sends it), ... .

This theoretically (I don't know the exact implementation of fetch-node so I cant say it for sure) should enable the possibility to use it to abort the fetch of the body in case it is too large.

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