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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:55:31+00:00 2026-05-24T04:55:31+00:00

After playing with Node.js and reading about async i/o & evented programming a lot

  • 0

After playing with Node.js and reading about async i/o & evented programming a lot I’m left with some question marks.

Consider the following (pseudo) code:

var http = require('http');

function onRequest(request, response)
{
    // some non-blocking db query
    query('SELECT name FROM users WHERE key=req.params['key']', function (err, results, fields) {
        if (err) {
            throw err;
        }
        username = results[0];
    });

    // some non-blocking db query
    query('SELECT name FROM events WHERE key=req.params['key']', function (err, results, fields) {
        if (err) {
            throw err;
        }
        event_name = results[0];
    });

    var body = renderView(username, event_name, template);
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write(body);
    res.end();
};

http.createServer(onRequest).listen(8888);

// request A: http://127.0.0.1:1337/?key=A
// request B: http://127.0.0.1:1337/?key=B

(I think) I understand the basics of the event loop; With libev, Node.js creates an event loop that polls (epoll/kqueue/…) a bunch of file descriptors to see if any events are triggered (new connection, writable, data available etc). If there is a new request the event loop calls the anonymous function passed to createServer. What I don’t understand is what happens after:

1) To run the queries concurrently the db driver has to have some kind of threading/connection pool, right?

2) In the scope of one request: What happens after sending two queries? renderView can’t be called because the queries have not returned yet. How do we wait for the queries to return? Should it keep count of the callbacks pending to be fired before continuing? The basic thought I had was;

onRequest -> run async code -> wait for callbacks -> construct response. The waiting in this case would be blocking so you would effectively need to spawn a thread for each onRequest. How is the “waiting for callbacks to run before constructing response” done?

3) How does the db driver inform the event-loop that it’s done and the callback it has for it needs to be called with the query results?

4) How does the event loop run the callback inside the anonymous function we created with the onRequest event? Is this where the closure concept comes in where the context is “saved” in the callback function?

4) Now that we have the db results, how do we continue executing the renderView/res.write/res.end parts?

  • 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-24T04:55:31+00:00Added an answer on May 24, 2026 at 4:55 am

    Run parrallel async code pattern:

    To ‘Wait for result from both async functions’ you can do: in both async calls callbacks check both result and if all ready, call your DoSomethingWithTwoDependantResults.

    In your example you probably need to execute queries sequentially:

    query(sql1, function(sqlres1) {
        query(sql2, function(sqlres2) {
            writeResultUsingDataFrom(sqlres1, sqlres2);
        }
    });
    

    your original code, modified to execute two queries in parallel:

    function writeReply(res, template, username, event_name)
    {
        var body = renderView(username, event_name, template);
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write(body);
        res.end();
    } 
    
    function onRequest(request, response)
    {
        // some non-blocking db query
        query('SELECT name FROM users WHERE key=req.params['key']', function (err, results, fields) {
            if (err) {
                throw err;
            }
            username = results[0];
            if (username && event_name)
                writeReply(res, template, username, event_name);
        });
    
        // some non-blocking db query
        query('SELECT name FROM events WHERE key=req.params['key']', function (err, results, fields) {
            if (err) {
                throw err;
            }
            event_name = results[0];
            if (username && event_name)
                writeReply(res, template, username, event_name);
    
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question , I was reminded of when I was taught Java
After reading a bit more about how Gnutella and other P2P networks function, I
I've used DbUnit but after playing about with the Play Framework recently I've found
I'm playing with Remote Actors but I'm facing some difficulties. Consider this server: object
I've noticed some memory leaks in an app i'm building, after playing around with
After playing around with Vaadin for about a week I'm curious about how Vaadin
I am still kind of new to programming and after playing around with a
I just found /n softwares free Powershell NetCmdlets, and after playing with them I
I've been playing this flash game and after I'd gotten over the initial ('lol,
After reading the Head First Design Patterns book and using a number of other

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.