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
|
Show 2 more comments
3 Answers
Reset to default 19The 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));
u.json()
returns a promise – Jaromanda X Commented Dec 12, 2016 at 23:36.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:18request()
module would be a much more powerful option for making http request in node.js. – jfriend00 Commented Dec 13, 2016 at 1:26node-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