21

As per the node-fetch documentation node-fetch

we can get the response status like this

fetch('https://github.com/')
    .then(res => {
        console.log(res.status);
    });

and for getting the data

fetch('https://api.github.com/users/github')
    .then(res => res.json())
    .then(jsonData => console.log(jsonData));

I have a scenario where I need to return the JSON data and the status from the response. I tried to use like this

     fetch('https://api.github.com/users/github')
            .then(res => res.json())
            .then(jsonData => {
             console.log(jsonData);
             console.log(jsonData.status);
      });

but the

console.log(jsonData.status)

won't return the status. How I can get status and output data

NAVIN
  • 3,193
  • 4
  • 19
  • 32
Dony Joseph
  • 499
  • 2
  • 5
  • 10
  • Check this [answer](https://stackoverflow.com/a/51972767/2767755). It has the hint how to do this. – Arup Rakshit Aug 22 '18 at 19:36
  • Possible duplicate of [How do I access previous promise results in a .then() chain?](https://stackoverflow.com/questions/28250680/how-do-i-access-previous-promise-results-in-a-then-chain) – Arup Rakshit Aug 22 '18 at 19:37

4 Answers4

39

The easiest solution would be to declare a variable and assign res.status value to it:

let status; 
fetch('https://api.github.com/users/github')
  .then((res) => { 
    status = res.status; 
    return res.json() 
  })
  .then((jsonResponse) => {
    console.log(jsonResponse);
    console.log(status);
  })
  .catch((err) => {
    // handle error
    console.error(err);
  });

You can also try it that way using async/await:

const retrieveResponseStatus = async (url) => {
  try {
    const response = await fetch(url);
    const { status } = response; 
    return status;
  } catch (err) {
   // handle error
    console.error(err);
  }
}

Then You can use it with any URL You want:

const status = await retrieveStatus('https://api.github.com/users/github')

KarlR
  • 1,545
  • 12
  • 28
14

Another alternative solution is using Promise.all

fetch('https://api.github.com/users/github')
  .then(res => Promise.all([res.status, res.json()]))
  .then(([status, jsonData]) => {
    console.log(jsonData);
    console.log(status);
  });

Hope it helps

deerawan
  • 8,002
  • 5
  • 42
  • 51
0

I might be going in opposite direction however I would suggest you to use requestJs, as its community is much bigger then node-fetch and provides lots of functionality.

With requestJs you can fetch statusCode like

 request("https://api.github.com/users/github", (err, res, body) => {
     let statusCode = res.statusCode;
 });

RequestJs also provides lots of easy way to call different API methods and also easy to watch API request and API response.

NAVIN
  • 3,193
  • 4
  • 19
  • 32
-1

Here is yet another variant, using async lambda functions and array destructuring:

fetch('https://api.github.com/users/github')
  .then(async (res) => {  
    return [await res.json(), res.status];
  })
  .then(([jsonData, status]) => {
    console.log(jsonData);
    console.log(status);
  })
  .catch((err) => {
    // handle error
    console.error(err);
  });
Nicolai Weitkemper
  • 403
  • 1
  • 9
  • 18