-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeschool9.js
49 lines (38 loc) · 982 Bytes
/
nodeschool9.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var http = require('http');
function getURL (url, callback) {
// var dataStr = ''; ** declare dataStr here if I want it to accumulate in the below URLS.foreach
// var self = this; ** use self.dataStr within URLEvent to call it
http.get (url, function (response){
var dataStr = '';
response.setEncoding('utf8');
response.on('data', function(data) {
dataStr += data;
});
response.on('end', function() {
callback(null, dataStr);
});
response.on('error', function(err) {
callback(err);
});
});
}
var URLS = [
[process.argv[2] , ''],
[process.argv[3] , ''],
[process.argv[4] , '']
];
var callbackCounter = 3;
URLS.forEach(function (url,i,arr) {
getURL(arr[i][0], function (err, data){
if (err){
throw err;
}
arr[i][1] = data;
callbackCounter -= 1;
if (callbackCounter === 0){
arr.forEach( function(urls) {
console.log(urls[1]);
});
}
});
});