javascript - Node-fetch returns Promise { <pending> } instead of desired data -


this question has answer here:

i trying fetch json website using node-fetch module, , have made following function:

var fetch = require("node-fetch");  function getjson(url) {   return fetch(url)     .then(function(res) {       return res.json();     }).then(function(json) {       //console.log(json) logs desired data       return json;   }); }  console.log(getjson("http://api.somewebsite/some/destination")) //logs promise { <pending> } 

when printed console, receive promise { <pending> } however, if print variable json command line last .then function, desired json data. there way return same data?

(i apologize in advance if misunderstanding issue on part, rather new javascript)

a javascript promise asynchronous. function not.

when print return value of function return promise (which still pending).

example:

var fetch = require("node-fetch");  // demonstational purpose, function here redundant function getjson(url) {   return fetch(url); }  getjson("http://api.somewebsite/some/destination") .then(function(res) {   return res.json(); }).then(function(json) {   console.log('success: ', json); }) .catch(function(error) {   console.log('error: ', error); }); 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -