I'm having difficulty accessing the result of an asynchronous request to an rss feed. I referred to the post, How do I return the response from an asynchronous call?, which suggests using Promises. I implemented a Promise, which resolves, but still cannot access the result of the request outside the request function.
I am attempting to set the variable film
to the request result, but can only seem to get undefined
.
var request = require("request");
var parseString = require("xml2js").parseString;
var url = 'http://feeds.frogpants.com/filmsack_feed.xml';
var film;
request(url, function(error, response, body) {
return new Promise(function(resolve, reject) {
if (error) {
console.log(error);
}
else {
if (response.statusCode === 200) {
parseString(body, function(err, result) {
var feed = result.rss.channel[0].item.reduce(function(acc, item) {
acc.push(item);
return acc;
}, [])
resolve(feed)
})
}
}
})
.then(function(data) {
film = data;
})
})
console.log(film)
Logging feed
from within the request function returns the results that I am looking for. What am I missing? Thanks.