1

I was trying this as a tutorial from nodeschool, and I'm new to Node.js. The code is below, and I know the problem in it, but I am not able to resolve it. The problem is that j has a value of 3 for every iteration of the loop inside the bl function, but Why is that happening?

var bl = require('bl');
var http = require('http');
var urls = process.argv.slice(2);



var result = [];

for (var i = 0; i < urls.length; i++) {
    result.push(null);
}

for(j = 0 ; j < urls.length; ++j){
    http.get(urls[j],function(response){
        response.pipe(bl(function(err,data){
            //console.log(result[i]);
            //console.log(data.toString());
            result[j] = data.toString();
            console.log('J : ' + j);
            console.log(data.toString());
            var flag = 0;
            for (var i = 0; i < result.length; i++) {
                console.log('here1');
                console.log(result[i]);
                if(result[i] == null){
                    flag = 1;
                    console.log('here');
                    break;
                }
            }
            if(flag == 0){
                for (var i = 0; i < result.length; i++) {
                    console.log(result[i]);
                }
            }
        }));
    });
}
3
  • 1
    See stackoverflow.com/questions/30436985/… Commented Jun 28, 2015 at 7:26
  • @Pranav Sharma how do you run this application? Commented Jun 28, 2015 at 9:07
  • @GiuseppePes Its a part of tutorial from nodeschool, learnyounode. This is one of the core workshop and is run into command line. Commented Jun 28, 2015 at 12:33

1 Answer 1

1

http.get is an async request but the for is sync so for is "most fast" and when http.get finish to download the url data then variable "j" takes last value.

You have another mistake I think, on your for loop you increase variable "j" as "++j" and it will be "j++".

To fix the first issue (variable "j" value) you can use an anonymous function and pass the value "j" like:

for(j = 0 ; j < urls.length; j++) {
    (function(j) {
        http.get(urls[j],function(response){
            response.pipe(bl(function(err,data){
                //console.log(result[i]);
                //console.log(data.toString());
                result[j] = data.toString();
                console.log('J : ' + j);
                console.log(data.toString());
                var flag = 0;
                for (var i = 0; i < result.length; i++) {
                    console.log('here1');
                    console.log(result[i]);
                    if(result[i] == null){
                        flag = 1;
                        console.log('here');
                        break;
                    }
                }
                if(flag == 0){
                    for (var i = 0; i < result.length; i++) {
                        console.log(result[i]);
                    }
                }
            }));
        });
    }(j));
}

There are a lot of code but in resume i did this:

for(j = 0 ; j < urls.length; j++) {
    (function(j) {
        /* your code inside this function will have the correct 
        value to variable "j" if you use async methods */
    } (j));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or use urls.forEach().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.