I have a function that returns a promise. In the first block of the promise, I fetch some XML and write it to a file called feed.xml. Then, in the next block, I convert that XML into JSON. The problem here is that my function tries to convert the xml file to JSON before the file is finished writing to my system. How can I make the second block wait on the first block?
Here is my function:
var out = fs.createWriteStream('./feed.xml');
var fetchJSON = () => {
return new Promise((resolve, reject) => {
var feedURL = 'http://www2.jobs2careers.com/feed.php?id=1237-2595&c=1&pass=HeahE0W1ecAkkF0l';
request(feedURL).pipe(zlib.createGunzip()).pipe(out);
resolve();
}).then(() => {
fs.readFile('feed.xml', function(err, data) {
parser.parseString(data, function(err, result) {
console.log(result);
});
});
});
};
fetchJSON();