node.js, can't get response from callback when execute function on a for loop -
i want make simulation log in web application multiple users in node.js
have function login(), , execute in loop simulate log in many users.
when maxthreads > 5, sometimes don't receive response callback request in function login() 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()+ "-" + ); }); } 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 have more threads - have more null responses
you use async module instead of loop. way you'd have more control on control flow.
Comments
Post a Comment