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

  • Home
  • SEARCH
  • 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 8854805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:58:06+00:00 2026-06-14T13:58:06+00:00

I was going through this article, i have one particular doubt if some one

  • 0

I was going through this article, i have one particular doubt if some one clear out to me.

http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb

var fs = require('fs')
  , sys = require('sys');

fs.readFile('treasure-chamber-report.txt', function(report) {
  sys.puts("oh, look at all my money: "+report);
});

fs.writeFile('letter-to-princess.txt', '...', function() {
  sys.puts("can't wait to hear back from her!");
});

Your code gives node the two tasks to read and write a file, and then goes to sleep. Once node has completed a task, the callback for it is fired. But there can only be one callback firing at the same time. Until that callback has finished executing, all other callbacks have to wait in line. In addition to that, there is no guarantee on the order in which the callbacks will fire.

“So I don’t have to worry about code accessing the same data structures at the same time?”
You got it! That’s the entire beauty of JavaScripts single-threaded / event loop design!

  1. Can anyone explain me the above bold lines.? How come we are not able to worry that the two different programs would not access the object.
  2. How come the current way of threading has problem?
  3. Will the order of firing the callback’s be a problem? Let’s take i want callBack A() to return first before callBack b().
  • 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-14T13:58:08+00:00Added an answer on June 14, 2026 at 1:58 pm

    1) If you’re running single threaded, you don’t have to worry about the problems that come with a multi-threaded application. This includes 2 different threads trying to use the same object at the same time. Imagine, for example, if one thread was trying to read data from a hash, while another thread was deleting data from the same hash. A key/value pair may look like it was present in one line of code, but because of threading by the time it reaches the next line the data may not be there anymore. Similarly, you don’t have to deal with all the extra code and headaches involved in avoiding these problems.

    2) See #1. It’s not a problem so much as a tradeoff. Lots of computers these days have multiple processors/cores, so having a program use more than one thread at a time can be beneficial. It is also useful when you expect a thread to be blocking. For example, in another multi-threaded language it would be common to read the contents of the file, and the output them without the addition of a callback. However, this means that thread sits there doing nothing (blocked) until the file read operation is done. Multi-threaded programming is also very hard to do correctly.

    3) You wouldn’t not be gauranteed order. If you want to ensure proper order, you wait to execute the 2nd call until the first has returned. e.g.

    fs.readFile('treasure-chamber-report.txt', function(report) {
        sys.puts("oh, look at all my money: "+report);
    
        fs.writeFile('letter-to-princess.txt', '...', function() {
          sys.puts("can't wait to hear back from her!");
        });
    });
    

    Note that this can sometimes get you into what is commonly referred to as ‘callback hell’

    EDIT: To address your comments:

    1) Even though you are “waiting” for NodeJS for the file to be read, in NodeJS this is a non-blocking operation. That means the method call (readFile) returns immediately, even before the file is read. This is because it passes off the data IO request to the underlying operating system, which has it’s own threads for processing such requests. When the operating system finishes the read (or write), it notifies the originating process that it is ready with the data. While the operating system is doing this work, NodeJS can let it’s one thread continue on doing other work while it waits. That’s why you need the callback – you need a way to tell NodeJS what to do next when you finally get the data.

    2) There is nothing inherently bad about callback hell, other than it is hard to read. One might imagine that if what you are trying to do includes several different asynchronous processes (like reading and writing to disk), that you might get something that looks like this:

    var doSomething function(){
        fs.readFile('step1.txt', function(result){
            // do something with the result
            fs.writeFile('step2.txt', function(){
                // okay, step2 is ready, so process that
                fs.readFile('step2.txt', function(result){
                    fs.writeFile('step3.txt', function(){
                        //etc, etc
                    });
                });
            });
        });
    }
    

    You can see that nesting can get rather deep rather quickly, and get difficult to read. If you search for “JavaScript callback hell” there are many discussions here and elsewhere that talk about this. One approach to flatten things out is to avoid inline/anonymous functions, and flatten it out with named functions, so you callbacks are not nested so deep in your editor (though the nesting is still happening from a lexical standpoint).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was going through this article : http://www.ajaxline.com/32-tips-to-speed-up-your-mysql-queries at point#4 , it states :
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore
In C following this article ( http://blogs.msdn.com/oldnewthing/archive/2006/12/21/1340571.aspx ), we have succesfully been able to
I'm going through the Sharded Counters example in Java: http://code.google.com/appengine/articles/sharding_counters.html I have a question
I was going through this article and got stuck with one issue. Imagine I
I am going through this tutorial about PDO and have come to the point
I was going through some data structures and I noticed this as a time
I have been going through the web about this exception. I found many articles
I was going through this article to understand more about Java Serialization process. When
I am going through this tutorial , but I already had Ruby 1.8.7 installed.

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.