1

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

Community
  • 1
  • 1
Chris
  • 443
  • 1
  • 5
  • 13
  • Your work on this will become a lot easier if you can use Promises and Node 7, which has `async` and `await` keywords. Then you can simply use a traditional `for` loop exactly like you are used to. https://blog.risingstack.com/async-await-node-js-7-nightly/ – Seth Holladay Jan 05 '17 at 04:49
  • this is all command line on fedora box, so i can probably use anything and everything, i will give it a look – Chris Jan 05 '17 at 05:29

2 Answers2

0

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.

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
0

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.

Dmytro Yashkir
  • 1,225
  • 12
  • 12
  • i am more than certain i placed the callback in the event response function as well, but i will check it again thanks – Chris Jan 05 '17 at 05:28
  • i actually did do what you suggested and the code hangs on the second request and just times out or it spits out if (fn === null) throw new Error("Callback was already called."); – Chris Jan 05 '17 at 05:40
  • Then there is an issue with the server.on call, is that an http library like request? https://github.com/request/request. Callback should definitely be in that spot, but perhaps the RequestResponse is not exactly what you need. Can you edit the question with the exact non-simplified code. The Error message suggests that .on is being called multiple times like, client receiving chunks of data maybe – Dmytro Yashkir Jan 05 '17 at 05:58
  • well right the csv is about 100 lines long and i am running the request on each line and need to get the event for each request, before i start the next request and then wait for its event response before the next so on and so forth. I guess what you are saying is that I should find another way? idk. – Chris Jan 05 '17 at 06:06
  • also this isnt a typical request it is a call to the steam game coordinator with an event response – Chris Jan 05 '17 at 06:08
  • I think I know what the problem is, since server is a single emitter. We are registering multiple callback on same event. Need to use Server.removeAllListeners("ServerResponse") before making another request – Dmytro Yashkir Jan 05 '17 at 06:24
  • I appreciate all your communique and help brother, but it stalls after the first iteration. I will dive into the steam code tomorrow, and maybe there will be other answers – Chris Jan 05 '17 at 06:44