Saturday 27 May 2017

How does Node know how to execute the functions in this program in order?

In this program, how does Node know to the execute the for loop at the bottom, and then the middle function, and then the top function even if they're written in the wrong order? I get node is async, but I don't get how this works. var http = require('http') var bl = require('bl') var results = [] var count = 0function printResults () { for (var i = 0; i < 3; i++) console.log(results[i]) } function httpGet (index) { http.get(process.argv[2 + index], function (response) { response.pipe(bl(function (err, data) { if (err) return console.error(err) results[index] = data.toString() count++ if (count == 3) printResults() })) }) } for (var i = 0; i < 3; i++) httpGet(i) ` Why doesn't it just print undefined for the values of results array?Ugh, reddit formatting sucks, here's the code.

Submitted May 27, 2017 at 07:44PM by jimmypixel

No comments:

Post a Comment