1

UPDATE: I am currently researching and trying to implement this using a Promise. It isn't working just yet but I believe I am getting close. If anyone has any input about a Promise is a better way to go than settimeout I would appreciate your feedback.

I am currently using the feedsub module that reads an rss feed and sends the data back to my app.

Currently my code looks like:

var reader = new FeedSub('http://pathtosite/atom.xml', {
        emitOnStart: true
    });                 

reader.read(function(err,items){
                atomXMLposts = items;
                console.log(atomXMLposts);
});

The reader.read accepts a callback function that runs when it receives the items argument.

the console.log(atomXMLposts) logs the items in an array, which is what I want. But...

When I run this block of code right after:

atomXMLposts.forEach(function(post){
                // console.log(post["published"]);
                var now = moment(post["published"]);
                console.log(now);
            })

I receive an error saying TypeError: Cannot call method 'forEach' of undefined

I believe this is a result of of the function running before atomXMLposts is assigned the values. So I wrapped a setTimeout around this code block.

setTimeout(function(){

    // if(atomXMLposts)
    atomXMLposts.forEach(function(post){
        // console.log(post["published"]);
        var now = moment(post["published"]);
        console.log(now);
    })
}, 2500);

This returns values but I personally feel using setTimeout is not a good practice. The 2500 ms I added is an arbitrary number I guesstimated. What would be a better way of handling this? Is there a better way? I am trying to search for a better alternative, when I search using setTimeout to wait for parameters or something to that nature, I am not receiving anything that is useful.

HelloWorld
  • 10,529
  • 10
  • 31
  • 50
  • You could just use `0` for the `setTimeout()` function. No need in unnecessary delay. – lucusp Sep 30 '14 at 03:32
  • When it is 0, atomXMLposts is undefined, it needs time to be populated and be sent the RSS feed data. – HelloWorld Sep 30 '14 at 03:34
  • You've tested this? It should still be an asynchronous call. – lucusp Sep 30 '14 at 03:37
  • My current code works with the 2500ms,when I insert `0` I get `TypeError: Cannot call method 'forEach' of undefined` – HelloWorld Sep 30 '14 at 03:44
  • Gotcha, sometimes my brain only functions half the time :/ – lucusp Sep 30 '14 at 03:54
  • Ultimately what you asked is, should you use setTimeout, and the answer is that it's perfectly fine. You could use your own wrapper function also. – lucusp Sep 30 '14 at 03:59
  • What would happen if the request took more than 2.5 seconds? It would still be undefined. I am currently researching and trying to learn how to implement a promise. From what I am reading this is the better way to go but still not 100% sure. – HelloWorld Oct 01 '14 at 13:11
  • You should just place the code inside the `reader.read(function(err,items){` callback instead of a timeout callback. If you want, you can also use promises on top of that. – Bergi Oct 01 '14 at 13:44

0 Answers0