Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!