I have written a small code snippet to fetch a quote from the third party service -
var http = require("https");
function getRandomQuote()
{
var returnJson = {};
var options = {
"method": "GET",
"hostname": "talaikis.com",
"port": null,
"path": "/api/quotes/random/",
};
http.get(options, function(resp){
resp.on('data', function(chunk){
console.log("Quote string - "+chunk.toString('utf8'));
returnJson = JSON.parse(chunk.toString('utf8'));
console.log(returnJson);
return returnJson;
});
resp.on("error", function(e){
console.log("Got error: " + e.message);
});
});
}
var x = getRandomQuote();
console.log(x);
The output is -
{}
Quote string - {"quote":"Such an arrangement would provide Taiwan and China with a forum for dialogue whereby they may forge closer ties based on mutual understanding and respect, leading to permanent peace in the Taiwan Strait.","author":"Nick Lampson","cat":"respect"}
{ quote: 'Such an arrangement would provide Taiwan and China with a forum for dialogue whereby they may forge closer ties based on mutual understanding and respect, leading to permanent peace in the Taiwan Strait.',author: 'Nick Lampson',cat: 'respect' }
Although the correct output is received it isnt returned in the function. How do I fix this?