-
Notifications
You must be signed in to change notification settings - Fork 0
/
each.js
31 lines (28 loc) · 1.21 KB
/
each.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
const http = require("http"); //Package used to make http request
const async = require("async"); //Package used for async operation handling
//the callback that will be used for async calling
//url - url passed to call the http server
//next - this will be a callback provided by async library so that we can call next function in async chain
const httpCallback = (url, next) => {
http.get(url, res => {
//Get method makes an http get request to the given url
let httpResult = "";
// on method sets up a 'data' event listner on the http response (as the response comes in chunks)
res.on("data", chunk => {
//concatenating the http response
httpResult += chunk.toString();
});
//setting up clean up listeners for each event (end -on the http response stream end, err - on http response stream error)
res
.on("end", () => {})
.on("err", err => {
//invokes the main callback with error object directly
next(err);
});
});
};
//using async.each method that calls an async function on each element of array
async.each([process.argv[2], process.argv[3]], httpCallback, err => {
//result of each callback is lost in case of async.each
console.log(err);
});