0

I was learning sequential execution in node.js and came across an interesting problem.

Below is my code. Basically, I have 5 RSS Feeds in an array and I am trying to output titles of them sequentially. For this purpose, I have created an array of functions and utilizing this helper function from a book:

function next(err, result)
{
    if(err) throw new Error(err);

    var currentTask = tasks.shift();

    if(currentTask)
    {
        currentTask(result);
    }

}

Now, my program works correctly, If I prepare array as :

tasks = [

function(){handle(urls[0])},
function(){handle(urls[1])},
function(){handle(urls[2])},
function(){handle(urls[3])},
function(){handle(urls[4])}

];

However it outputs in random order and not sequential, if I prepare as:

tasks = [

handle(urls[0]),
handle(urls[1]),
handle(urls[2]),
handle(urls[3]),
handle(urls[3]),

];

What is the difference between above 2 arrays?

Full Code:

var request = require('request');
var parser = require('htmlparser');

var urls = ['http://psychcentral.com/blog/feed/rss2/', 'http://davidicuswong.wordpress.com/feed/', 'http://www.theglobalconversation.com/blog/?feed=rss2', 'http://happiness-project.com/feed', 'http://www.marriagemissions.com/feed/'];

function handle(url)
{
    request(url, function(error, response, body)
    {
        if (!error && response.statusCode == 200)
        {
            var handler = new parser.RssHandler();
            var rssParser = new parser.Parser(handler);

            rssParser.parseComplete(body);

            if (handler.dom.items.length)
            {
                var item = handler.dom.items.shift();
                console.log(item.title);
                console.log(item.link);
            }

           next(null, null);
        }


    });

}

function next(err, result)
{
    if(err) throw new Error(err);

    var currentTask = tasks.shift();

    if(currentTask)
    {
        currentTask(result);
    }

}


tasks = [

function(){handle(urls[0])},
function(){handle(urls[1])},
function(){handle(urls[2])},
function(){handle(urls[3])},
function(){handle(urls[4])}

];

next();
5
  • 1
    Well, the first is an array of functions, while the second is an array of undefineds? Commented Aug 6, 2014 at 17:53
  • Yes I agree, but how come it still executes and outputs in random order though... Commented Aug 6, 2014 at 17:54
  • Well, you are executing it (not the next() queue), and they execute in parallel. Commented Aug 6, 2014 at 17:55
  • aah, I see ... it was so simple .. but I missed .. Thanks Commented Aug 6, 2014 at 18:04
  • You can put this as an answer.. I will be glad to accept Commented Aug 6, 2014 at 18:07

1 Answer 1

1

What is the difference between above 2 arrays?

Well, the first is an array of functions, while the second is an array of undefineds.

but how come it still executes and outputs in random order though?

You are executing it (not the next() queue), and they execute in parallel, so the async callbacks come back in non-deterministic order.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.