Learnyounode - Juggling Async

Howdy all,

I’m trying to make three async GET requests and have them print in the correct order. I can’t see where my code is lacking (as per the learnyounode spec, I don’t want to pull in a module).

The code takes three URLs when running (for the GET requests). I have tested this with multiple trial URLs, and sometimes the request just doesn’t doesn’t come back. Can you see any issue with my code?

var http = require('http');

var dataCount = 0;

var data = ["", "", ""];

for (let i = 2; i < process.argv.length; i++) {
    doGet(i - 2, process.argv[i]);
}

function storeGet(strNum, response) {
    data[strNum] = response;
    dataCount++;
    
    // If all get requests have returned, prints data in order
    if (dataCount == 3) {
        data.forEach(function(element) {
            console.log(element);
        });
    }
}

function doGet(strNum, url) {
    http.get(url, (results) => {
        results.setEncoding('utf8');
        results.on('error', console.error);
        results.on('data', function(data) {
            storeGet(strNum, data);
        });
    });
}
for (let i = 2; i < process.argv.length; i++) {
    doGet(i - 2, process.argv[i]);
}

The first call of doGet(i - 2, process.argv[i]) is === doGet(0, process.argv[0])

FYI first two process.argv are used for calling Node, not for passing urls