node.js, can't get response from callback when execute function on a for loop

I want to make a simulation like log in to web application multiple users in node.js I have a function login(), and I execute it in a for loop to simulate log in many users. But when maxThreads > 5, sometimes I don't receive response from callback to request in function login() My code is:

var request = require("request");
var cheerio = require("cheerio");


function login(i){
request({
  uri: "http://example.com/sign_in",
  method: "POST",
  timeout: 1000,
  followAllRedirects: true,
  form: {
    email: "mail@example.com",
    password: "password"
  }      
}, function (error, response, body) {
    var $ = cheerio.load(body);
    var title = $("title");
    console.log(title.html()+ "-" + i );             
});
}

var maxThreads = 10
for(i=0; i<maxThreads; i++){
  login(i)
}

Example output:

null-5
null-6
null-7
null-8
null-9
Exapmle title-0
Exapmle title-1
Exapmle title-3
Exapmle title-2
Exapmle title-4

My problem: whan I have more threads - I have more null responses

Answers


You could use the async module instead of a for loop. This way you'd have more control over the control flow.


Need Your Help

Profile Page PHP + MySQL

php mysql dynamic user-profile

The profile page is using a PHP and MySQL to display the item.