user1665355
user1665355

Reputation: 3393

Random order in async node.js

I now use this code (part of my code):

async.forEachOfSeries(dates, function(mydate, m, eachDone) { 
        eachDateParse(Name,Place,Strategy, eachDone)
}, function(err) {
    if (err) throw err;
    console.log("All Done!");
    callback(); }
); 

async.forEachOfSeries does loop over dates in order, but is there any async function that can randomise the order of the loop over dates?

Best Regards

Upvotes: 0

Views: 878

Answers (1)

kriskot
kriskot

Reputation: 303

This is an example for my comment as requested

var dates = ['20110101', '20120101', '20130101', '20140101', '20150101'];

async.sortBy(dates, function(item, callback) {
    callback(null, Math.random());
}, function(err, result) {
    console.log('Sorting is finished.');
    async.each(result, function(date, callback) {
        console.log('Parsing date: ' + date);
        callback();
    }, function(error, res) {
        console.log('All dates are parsed now.');
    });
});

although I'm not quite sure if nesting async functions is a good idea it seems to be working.

Upvotes: 2

Related Questions