最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node-fetch only returning promise pending - Stack Overflow

programmeradmin1浏览0评论

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())})

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())})
Share Improve this question asked Dec 12, 2016 at 23:28 LiondancerLiondancer 16.5k54 gold badges163 silver badges262 bronze badges 7
  • u.json() returns a promise – Jaromanda X 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() – Liondancer 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. – jfriend00 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 – Liondancer 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. – jfriend00 Commented Dec 13, 2016 at 1:31
 |  Show 2 more comments

3 Answers 3

Reset to default 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

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.

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));
发布评论

评论列表(0)

  1. 暂无评论