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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:49:11+00:00 2026-05-30T08:49:11+00:00

The novice understanding of Node is that if I re-write synchronous, or in-line code,

  • 0

The novice understanding of Node is that if I re-write synchronous, or in-line code, to utilize functions / callbacks, I can ensure that my code is non-blocking. I’m curious how this works in terms of the event stack. The simple example from here: Don’t understand the callback – Stackoverflow is that this will block:

var post = db.query("select * from posts where id = 1");
doSomethingWithPost(post)
doSomethingElse();

While this wont:

callback = function(post){
doSomethingWithPost(post)
}

db.query("select * from posts where id = 1",callback);
doSomethingElse();

Ok, I understand that we should use callbacks. But in terms of the event stack why does this work? Javascript is single threaded.. in the first example line one makes use of an expensive and blocking I/O operation. Line 2 can’t execute till line one is done. Is this because line 2 requires information from line 1? Or is it because I/O events are just fundamentally blocking operations meaning that they seize control and don’t give it back until done…

In the second example the expensive I/O has been moved into a function, and we now have a callback function. Certainly the callback can’t execute until the I/O is done.. This would not change. So the difference in amount of time it takes to execute between one and two must primarily be what would happen if a second request hit the server.

If a second request hit example one, it wouldn’t be able to process until request 1 was done because of the blocking operation.. but in the example two.. does moving operations into functions automatically spawn child processes or act as multi-threaded? If Javscript is single threaded this would still pose a problem unless there was some way of doing parallel processing.. Does a function / callback only guarantee to be non-blocking IF we make use of non-blocking techniques like child processes, etc…

  • 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-30T08:49:13+00:00Added an answer on May 30, 2026 at 8:49 am

    Imagine you’re operating the cash register in a bakery. You handle your customers sequentially and synchronously, like this:

    1. Take order
    2. Tell baker to bake the bread
    3. Wait until bread is baked
    4. Charge money
    5. Deliver bread
    6. GOTO 1 — next customer

    That will be very slow. Now, try instead to take the orders sequentially, but handle your customers asynchronously:

    1. Take order
    2. Tell baker to bake the bread, and notify you when finished. When notified:
      1. Charge money
      2. Deliver bread
    3. GOTO 1 — next customer

    UPDATE: I refactored the above, so it more closely resembles a callback. You, the cashier, will hit step 3 immediately after giving the order to the baker. You will hit step 2.1 when the baker notifies you that the bread is ready.

    In this manner, you will still deliver as much bread – you can only sell as much bread as your baker can bake. But you can deal with your customers in a more efficient manner, because instead of idly waiting for an order to come back, you start handling the next customer.

    Now, you could go all sorts of fancy on this, and charge the money upfront, and tell the customer to pick up the bread at the other end of the desk, or something like that. I think Starbucks are pretty “evented” in this way. The cashier takes the order, issues a number of requests for stuff, and tells the customer to wait until everything is standing in the pickup area. Super-efficient.

    Now, imagine that your friend starts operating another cash register. He follows your async example. You can handle more customers, even quicker! Note that the only thing you had to do was to put your friend there and give him your workflow.

    You and your friend are two single-threaded event loops running in parallel. This is analog to two node.js processes taking requests. You don’t have to to anything complex to parallelize this, you just run one more event loop.

    So, no, “moving operations into functions” does not “automatically spawn child processes”. They are more akin to alarms — when this is finished, notify me and let me pick up at this point, “this point” being the code in your callback. But the callback will still be executed in the same process and the same thread.

    Now, node.js also operates an internal thread pool for IO. This is abstracted away from you: To continue the bakery analogy, let’s say you have a “baker pool” of bakers — to you, standing at the cash register, you don’t have to know about this. You just give them the order (“one sourdough loaf”) and deliver that order when you are notified that it’s finished. But the bakers are baking their bread in parallel, in their own “baker pool”.

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

Sidebar

Related Questions

I am a AS3 novice learning PureMVC and want to write code following best
Php novice. 1.Is there anything wrong with this PHP & MySQL code? include_once db_login.php
I am a novice programmer who is trying to teach myself to code, specifically
I am novice in sharepoint programming. I have a following code: SPWorkflowTask task =
I have a fairly novice understanding of CSS and HTML, and I'm trying to
Is my understanding correct, that the following class and table design is not possible
I have a piece of code that presents an interesting question (in my opinion).
I'm a novice Android developer. I used the code given at Google's Android developer
I'm having problems understanding how htaccess redirects work: Can I do a background redirect,
Im having problems understanding the app logic to this password reset code i found

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.