Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:33:50+00:00 2026-06-09T04:33:50+00:00

Hej, I’m experimenting with node.js and its callback mechanism. And now I wonder how

  • 0

Hej,

I’m experimenting with node.js and its callback mechanism. And now I wonder how to overhand data to such an anonymous callback:

var fs = require('fs');

for (var i = 0; i < 4; i++) {
    console.log('Outer: ' + i);
    fs.readFile('/etc/hosts', 'ascii', function(err, data) {
        console.log('Inner: ' + i);
    });
}

I do understand why the inner call to i returns 4 always. But how can I supply some variables to that specific readFile-callback function (so that the inner i will have the value of the outer i)? I could imagine some queue mechanism, where the callback function acts as a consumer, but as a node beginner I’d like to ask for the best practices. Thanks a lot,

mechko

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:33:51+00:00Added an answer on June 9, 2026 at 4:33 am

    With the pure Javascript (that is, not using libraries such as async):

    var fs = require('fs');
    
    var i,
        reader = function (i) {
            return function (err, data) {
                console.log('Inner: ' + i);
            }
        };
    
    for (i = 0; i < 4; i++) {
        console.log('Outer: ' + i);
        fs.readFile('/etc/hosts', 'ascii', reader(i));
    }
    

    Or, alternatively,

    var fs = require('fs');
    
    var i;
    
    for (i = 0; i < 4; i++) {
        console.log('Outer: ' + i);
        fs.readFile('/etc/hosts', 'ascii', function (i, err, data) {
            console.log('Inner: ' + i);
        }.bind(null, i));
    }
    

    (moving the var i; declaration out of the for loop does not really changes anything in this specific example, it is just a code style to prevent the bugs related to Javascript, as opposed to the programming languages like Java, having only the function scope for variables – so that, in your original example, i is declared for the entire module, not just for for loop).

    Alternative approach would be to use libraries such as async and underscore like this:

    var _ = require('underscore'),
        async = require('async'),
        fs = require('fs');
    
    async.parallel(_.map(_.range(0, 4), function (i) {
        return async.waterfall.bind(null, [
            fs.readFile.bind(null, '/etc/hosts', 'ascii'),
            function (data, callback) {
                console.log("Inner: " + i);
                callback();
            }
        ]);
    }, function (err) {
        if (err) {
            console.log("Some read attempt failed");
        } else {
            console.log("Done reading");
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This my method in my repository: public List<Question> getallquestion() { var hej = from
Hej, I have some data shipped out with the app which shall be copied
hej.h void hej(); hej.m void hej(){} main.mm #import hej.h int main(int argc, char *argv[])
Hej everyone, problem : I am looking for right way to convert an index
Just a blackout, I want the String hello hallo tjena hej tere to an
Hej folks, I'm currently completely stuck with the following and don't even have the
Hej I am trying to load an (embedded) image in a wpf application, using
I have this code, int main() { std::string st; std::stringstream ss; ss<<hej hej med
hej I got idea to compile lesscss files while building my project with maven.
Hej folks, I got an UIView in my application and want to implement kind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.