Tuesday 16 May 2017

async.eachSeries with Interval function

Basically I want to make an async.eachSeries run with a 5 second interval before it does another one....

I have 2 Functions... The 1st function reads through a Text file and stores each line value in an array. My array will have about 20 000 Items. It also calls the second function, and runs an async.eachSeries. The 2nd function takes the array (20 000) and separates each one into smaller arrays... (5000).

Below is my 1st Function:

var LineReader = reader.createInterface({
    input: fileReader.createReadStream('lines.txt')
});

LineReader.on('line', function (line) {
    var NewNumber = line.toString();
    number_list.push(NewNumber);
});

LineReader.on('close', function(){
    var chunkData = chunk(5000, number_list);
    async.eachSeries(chunkData, function(chunk, callback) {
        console.log(chunk.length);
        callback(null);
    })
});

Second Function

function chunk(chunk_size, array) {
    return array.reduce(function(previous, current) {
        var chunk;
        if (previous.length === 0 ||
            previous[previous.length - 1].length === chunk_size) {
            chunk = [];
            previous.push(chunk);
        } else {
            chunk = previous[previous.length - 1];
        }

        chunk.push(current);

        return previous;

    }, []);

}



via Greg Sithole

No comments:

Post a Comment