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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:18:27+00:00 2026-06-16T00:18:27+00:00

Below is a snapshot of my code from routes/index.js exports.index = function(req, res){ var

  • 0

Below is a snapshot of my code from routes/index.js

exports.index = function(req, res){
    var results=new Array();
    for(var i=0; i<1000;i++){
        //do database query or time intensive task here based on i 
        // add each result to the results array
    }
    res.render('index', { title: 'Home !' , results:results });
};

If I run this code , due to the asynchronous nature of javascript the last line gets executed before the loop gets completely processed . Hence my webpage doesnt have the results . How do I structure this such a way that the page gets loaded once the queries are completed ?


Updated

Inside the loop I have database code ( Redis ) like the below –

client.hgetall("game:" +i, function(err, reply) {

           results.push(reply.name);
        });
  • 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-16T00:18:28+00:00Added an answer on June 16, 2026 at 12:18 am
    exports.index = function(req, res) {
      var events = require("events");
      var e = new events.EventEmitter();
      e.expected = 1000;
      e.finished = 0;
      e.results = [];
    
      e.on("finishedQuery", (function(err, r) {
        this.finished += 1;
        this.results.push(r && r.name);
        if (this.finished === this.expected) {
           res.render('index', { title: 'Home !' , results:this.results });
        };
      }).bind(e));
    
      for (var i = 0; i < e.expected; i++) {
        client.hgetall("game:" + i, function(err, reply) {
          e.emit("finishedQuery", err, reply);
        });
      };
    };
    

    Of course, the above code doesn’t handle [1 or more] errors. You’d need to add logic that only responds (1) on the first error or (2) if no errors occur.

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

Sidebar

Related Questions

I am studying snapshot isolation level of SQL Server 2008 from the below link.
Below is the code from internalRegister method of GCMRegistrar class static void internalRegister(Context context,
below is the code to download a txt file from internet approx 9000 lines
Below is the code from a plugin I use for sitemaps. I would like
How can I change the name from 1.0.snapshot-jar-with-dependencies to something else, below are contents
Below is code to an inherited ComboBox. The issue is that the ComboBox is
Below code not work, but it's work fine for jsf1.2. Now the framework is
I need to get a regular snapshot from a webcam in Delphi. Speed is
//below In my application i have a button select image from sdcard and on
I trying to remove the html tags using the code below but it does

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.