Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"JUGGLING ASYNC" answer without BL. #449

Open
LautaroCuello opened this issue Jul 20, 2016 · 0 comments
Open

"JUGGLING ASYNC" answer without BL. #449

LautaroCuello opened this issue Jul 20, 2016 · 0 comments

Comments

@LautaroCuello
Copy link

LautaroCuello commented Jul 20, 2016

Hello, guys. I made a frankestein code to resolve this. (And it's bad but if you try various times it works)

var http = require("http");
var url = [process.argv[2], process.argv[3], process.argv[4]];
var responses = [];
var completed_req = 0;

for(var i in url){
    http.get(url[i], function(response){
        var content = "";
        //if(completed_req== url.length){
                response.setEncoding("utf-8");
                response.on("data", function(data){
                    content += data;
                })
                response.on("error", console.error);
                response.on("end", function(end){
                    console.log(content);
                });

    })
};

And the final solution was:

var http = require("http");
var bl = require("bl");
var results = [];
var count = 0;

function 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);

And! Surprise it didn't work when I copy it to my file. (I use Spanish language)

I hope it can be fixed (and get the right answer).

EDIT: For some what reason I skipped step 8, then BL now makes sense. I'm gonna close this to fell less stupid.

EDIT 2: After make it works on step 8 without using BL now I need to know how resolve without bl on step 9.

Step 8 results:

var http = require("http");
var url = process.argv[2];

http.get(url, function(response){
    var result = "";
    response.setEncoding("utf-8");
    response.on("data", function(data){
        result += data;
    });
    response.on("error", console.error);
    response.on("end", function(end){
        console.log(result.length);
        console.log(result);
    });
});

Official result:

var http = require("http");
var bl = require("bl");

http.get(process.argv[2], function(response){
    response.pipe(bl(function (err,data){
        if (err)
            return console.error(err);
        data = data.toString();
        console.log(data.length);
        console.log(data);
    }))
})
@LautaroCuello LautaroCuello changed the title "JUGGLING ASYNC" the answer is wrong! "JUGGLING ASYNC" the answer is wrong! (Solved) Jul 20, 2016
@LautaroCuello LautaroCuello reopened this Jul 20, 2016
@LautaroCuello LautaroCuello changed the title "JUGGLING ASYNC" the answer is wrong! (Solved) "JUGGLING ASYNC" answer without BL. Jul 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant