Node.js Iterate CSV File for Requests, but Wait for Response Before Continuing the Iterations

646 Views Asked by At

I have some code (somewhat simplified for this discussion) that is something like this

var inputFile='inputfile.csv';
var parser = parse({delimiter: ','}, function (err, data) {
    async.eachSeries(data, function (line, callback) {
            SERVER.Request(line[0], line[1]);
            SERVER.on("RequestResponse", function(response) {
                    console.log(response);
            });
            callback();
    });
});

SERVER.start()

SERVER.on("ready", function() {
    fs.createReadStream(inputFile).pipe(parser);
});

and what I am trying to do is run a CSV file through a command line node program that will iterate over each line and then make a request to a server which responds with an event RequestResponse and I then log the response. the RequestResponse takes a second of so, and the way I have the code set up now it just flies through the CSV file and I get an output for each iteration but it is mostly the output I would expect for the first iteration with a little of the output of the second iteration. I need to know how to make iteration wait until there has been a RequestResponse event before continuing on to the next iteration. is this possible?

I have based this code largely in part on NodeJs reading csv file

but I am a little lost tbh with Node.js and with async.foreach. any help would be greatly appreciated

2

There are 2 best solutions below

0
Jason Livesay On

I suggest that you bite the bullet and take your time learning promises and async/await. Then you can just use a regular for loop and await a web response promise.

7
Dmytro Yashkir On

Solution is straight forward. You need to call the "callback" after the server return thats it.

async.eachSeries(data, function (line, callback) {
        SERVER.Request(line[0], line[1]);
        SERVER.on("RequestResponse", function(response) {
                console.log(response);
                SERVER.removeAllListeners("RequestResponse", callback)
        });

})

What is happening is that eachSeries is expecting callback AFTER you are down with the particular call.