passing dynamic values in callback method

Posted by swastican on Stack Overflow See other posts from Stack Overflow or by swastican
Published on 2013-10-30T09:39:02Z Indexed on 2013/10/30 9:54 UTC
Read the original article Hit count: 134

Filed under:
|

is there a way to pass dynamic values to callback function in js or jquery or nodejs.

for(var i = 0; i < 10; i++)
{
  filename = 'file'+i+'.html';
  request(url, function(error, response, body) {
   test(error, response, body, filename);
  });

  function test(error, response, body, filename) {
   console.log('file name ' + filename);
   if(response.statusCode == 200){
    console.log('done');
   }
}

I refered this so article for passing values to callback function.

link: [JavaScript: Passing parameters to a callback function

the output always seems to be 9

How can i pass values dynamically? The callback function always refers the last value of filename.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about node.js