15

I am trying out node-fetch and the only result I am getting is:

Promise { <pending> }

How can I fix this so I get a completed promise?

code:

var nf = require('node-fetch'); nf(url).then(function(u){console.log(u.json())}) 
7
  • u.json() returns a promise Commented Dec 12, 2016 at 23:36
  • 3
    @jfriend00 i did not know .json() returns a promise so I thought it was my logic that was wrong and did not ask about .json() Commented Dec 13, 2016 at 1:18
  • FYI, I would think that the request() module would be a much more powerful option for making http request in node.js. Commented Dec 13, 2016 at 1:26
  • @jfriend00 thanks for the suggestion. I only went to node-fetch because a friend of my suggested it due to something about it being mainline in all browsers soon Commented Dec 13, 2016 at 1:28
  • Yeah, but there's usually no reason to choose a browser technology for use in node. I'd prefer to pick something designed to fully leverage all the capabilities in node which includes streams and the huge array of things you can include with an http request. I'd suggest you check out the request module. Commented Dec 13, 2016 at 1:31

3 Answers 3

19

The problem with your code is that u.json() returns a promise

You need to wait for that new Promise to resolve also:

var nf = require('node-fetch'); var url = 'https://api.github.com/emojis' nf(url).then( function(u){ return u.json();} ).then( function(json){ console.log(json); } ) 

For real code you should also add a .catch or try/catch and some 404/500 error handling since fetch always succeeds unless a network error occurs. Status codes 404 and 500 still resolve with success

Sign up to request clarification or add additional context in comments.

1 Comment

Would appending async/await to the internal inline function would be a more generic solution. Be it the function returned a promise or not?
7

A promise is a mechanism for tracking a value that will be assigned some time in the future.

Before that value has been assigned, a promise is "pending". That is usually how it should be returned from a fetch() operation. It should generally be in the pending state at that time (there may be a few circumstances where it is immediately rejected due to some error, but usually the promise will initially be pending. At some point in the future, it will become resolved or rejected. To get notified when it becomes resolved or rejected, you use either a .then() handler or a .catch() handler.

var nf = require('node-fetch'); var p = nf(url); console.log(p); // p will usually be pending here p.then(function(u){ console.log(p); // p will be resolved when/if you get here }).catch(function() { console.log(p); // p will be rejected when/if you get here }); 

If it's the .json() method that has you confused (no idea given the unclear wording of your question), then u.json() returns a promise and you have to use .then() on that promise to get the value from it which you can do either of these ways:

var nf = require('node-fetch'); nf(url).then(function(u){ return u.json().then(function(val) { console.log(val); }); }).catch(function(err) { // handle error here }); 

Or, with less nesting:

nf(url).then(function(u){ return u.json() }).then(function(val) { console.log(val); }).catch(function(err) { // handle error here }); 

There is an exact code sample for this on the documentation page for node-fetch. Not sure why you did not start with that.

Comments

1

u.json() returns a promise, so you'd need to do this

nf(url) .then(function(u){ return u.json(); }) .then(function(j) { console.log(j); }); 

or as you're using node

nf(url).then(u => u.json()).then(j => console.log(j)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.