'How to count callbacks?
I tried collect data from 3 sources but these are async. Therefore i tried to count callback. This is code which i wrote for this purpose.
var http = require('http');
var data_str1 = '';
var data_str2 = '';
var data_str3 = '';
var ended = 0;
function callback1(response){
response.on('end', function(){ended++;});
response.on('data', function(data){
data_str1 += data;
});
}
function callback2(response){
response.on('end', function(){ended++;});
response.on('data', function(data){
data_str2 += data;
});
}
function callback3(response){
response.on('end', function(){ended++;});
response.on('data', function(data){
data_str3 += data;
});
}
http.get(process.argv[2], function(response){
callback1(response);
if (ended == 3) console.log(data_str1);
});
http.get(process.argv[3], function(response){
callback2(response);
if (ended == 3) console.log(data_str2);
});
http.get(process.argv[4], function(response){
callback3(response);
if (ended == 3) console.log(data_str3);
});
Can you tell me what is the problem in this code?
Solution 1:[1]
const http = require('http');
const bl = require('bl');
const results = [];
let count = 0;
function printResults () {
for (let 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 (let i = 0; i < 3; i++) {
httpGet(i)
}
Solution 2:[2]
Problems here is
if (ended == 3) console.log(data_str2);
will run after callback2(response);
It not wait for response.on('end', function(){ended++;});
Solution 3:[3]
My solution, simple and straight to the point
const http = require('http')
const bl = require('bl')
const results = []
let count = 0
function printResults () {
for (let 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 (let i = 0; i < 3; i++) {
httpGet(i)
}
Solution 4:[4]
In your code check the ended value (ended===3) within the callback functions. I solved the problem using a similar approach.Note that I have used one callback function for all three urls
const http = require("http");
let url1 = process.argv[2];
let url2 = process.argv[3];
let url3 = process.argv[4];
let str1 = "";
let str2 = "";
let str3 = "";
let end = 0;
function cb(response, str) {
response.on("data", data => {
if (str === "str1") str1 += data;
if (str === "str2") str2 += data;
if (str === "str3") str3 += data;
});
response.on("end", () => {
end++;
if (end === 3) {
console.log(`${str1}\n${str2}\n${str3}`);
}
});
}
http.get(url1, response => {
cb(response, "str1");
});
http.get(url2, response => {
cb(response, "str2");
});
http.get(url3, response => {
cb(response, "str3");
});
Solution 5:[5]
My working solution with less code:
const http = require('http')
const [, , URL_1, URL_2, URL_3] = process.argv
const printData = (URL, cb) =>
() => http.get(URL, res => {
let data = ''
res.on('data', chunk => data += chunk.toString())
res.on('end', () => {
console.log(data)
cb()
})
})
printData(URL_1, printData(URL_2, printData(URL_3, null)))()
Solution 6:[6]
I know this is an old question, but I just wanted to share my ugly-ass solution with a different approach.
const http = require('http');
let data1 = '';
let data2 = '';
let data3 = '';
http.get(process.argv[2], (res) => {
res.on('data', (data) => {
data1 += data;
})
res.on('end', () => {
http.get(process.argv[3], (res) => {
res.on('data', (data) => {
data2 += data;
})
res.on('end', () => {
http.get(process.argv[4], (res) => {
res.on('data', (data) => {
data3 += data;
})
res.on('end', () => {
console.log(data1); console.log(data2); console.log(data3);
})
})
})
})
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | asv |
| Solution 2 | trquoccuong |
| Solution 3 | RustyShackleford |
| Solution 4 | Saravanan R. |
| Solution 5 | camalow |
| Solution 6 | Banzneri |
