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 6133863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:16:58+00:00 2026-05-23T17:16:58+00:00

I have the following code inside a class. (It’s coffeescript– and it’s for a

  • 0

I have the following code inside a class. (It’s coffeescript– and it’s for a couchdb utility!– but this is really a node.js question). I’m attempting to do things The Node Way, using Node 0.49, and that means using asynchronous calls for filesystem operations. At first, I was pulling my hair out because this.sentinel went to zero several times during the course of processing, so I know I’m doing something wrong there. But then I hit an even weirder issue: down in load_directory, see those console.log() calls? Watch when happens when I run this.

check_sentinel: ->
    @sentinel--
    if @sentinel == 0
        @emit('designDirLoaded', @object)

load_file: (rootdir, filename, object) ->
    @sentinel++
    fname = path.join(rootdir, filename)
    @manifest.push(fname)
    fs.readFile fname, (err, data) =>
        object[filename] = data
        @check_sentinel()

load_directory: (dirpath, object) ->
    @sentinel++
    fs.readdir dirpath, (err, files) =>
        for fname in files
            console.log("X1: ", fname)
            fs.stat path.join(dirpath, fname), (err, stats) =>
                console.log("X2: ", fname)
                if stats.isFile()
                    @load_file(dirpath, fname, object)
                if stats.isDirectory()
                    object[fname] = {}
                    @load_directory(path.join(dirpath, fname), object[fname])
        @check_sentinel()

Here’s what I get:

X1:  memberByName.js
X1:  memberByClub.js
X2:  memberByClub.js
X2:  memberByClub.js

This is surreal, and it looks a lot like a race condition. “memberByName” gets passed to fs.stat(), which in turn passes “memberByClub” to load_file(), implying… what? That because load_file() returned immediately, it raced around and presented the next file name in the array to the function call? Or do I have some misunderstanding about the persistence of values in a given scope?

  • 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-05-23T17:16:58+00:00Added an answer on May 23, 2026 at 5:16 pm

    No, what you see is expected. One thing you have to remember is that fs.stat is asynchronous. So, the outer loop (for fname in files) will finish looping before any of the callbacks to fs.stat is called.

    The reason why you see memberByClub.js twice is that you are using fname in the logging statement, but that variable is from the closure, which has changed by the time your callback to fs.stat is called.

    You can wrap the inner loop with do (fname) => to get the correct logging statements, but I think you need to restructure your code to achieve what you are trying to do with the whole class.

    load_directory: (dirpath, object) ->
        @sentinel++
        fs.readdir dirpath, (err, files) =>
            for fname in files
                do (fname) =>
                    console.log("X1: ", fname)
                    fs.stat path.join(dirpath, fname), (err, stats) =>
                        console.log("X2: ", fname)
                        if stats.isFile()
                             @load_file(dirpath, fname, object)
                        if stats.isDirectory()
                            object[fname] = {}
                            @load_directory(path.join(dirpath, fname), object[fname])
            @check_sentinel()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code inside a class: this.buttons = { cancelButton:{buttonId: cancelButton, buttonText:Cancel,
I have ASP.Net code similar to the following (this is inside a FIELDSET): <ol>
I have the following code inside a static method in a static class: Random
I have the following code inside my @interface FriendsNavController : UINavigationController class implementation. The
Inside a class of mine I have the following code: mHandler = createHandler(); private
I have the following code for creation of a node inside a graph. I'm
I have the following XAML code, which displays a picture (image inside borders) and
I have the following code inside a method: var list = new[] { new
I have the following code inside main method: List<Rectangle> rects = new List<Rectangle>(); for
I have the following code: public class NavigationPath { private string menuItems = <li>

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.