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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:46:42+00:00 2026-05-27T01:46:42+00:00

I’ve been exploring javascript more deeply lately, playing around with a node,redis,socket.io,express simple webapp.

  • 0

I’ve been exploring javascript more deeply lately, playing around with a node,redis,socket.io,express simple webapp.

I think I’ve come to a problem that is an example of the fundamental difference between JS and PHP, which is what I’ve been using the last few years.

I’ve written this callback and need to return a stack of data from redis

app.get('/deck', function(request, response) { 
  dealer.sort('cards', 'BY', 'deal:*->timestamp', 'GET', 'card:*->card_key',
    function(err, card_keys) {
      var deck = []; 
      for (k in card_keys) { 
        dealer.hget(card_keys[k], 'data', function(err, card_data) { 
          var deal = eval('(' + card_data +')');
          deals.push(cards);
        }); 
      }   
      response.json(deals);
    }   
  );  
});

Now first I thought it was a variable scoping problem so I rewrote with a closure (did I use this term correctly?) which didn’t work because I am writing this in synchronous mind set.
I realize that this is essentially synchronous and sends the data out before it is collected. The design is wrong.

I can rewrite the function to use socket.io’s emit function to send data as it is collected.

But are there design patterns for handling synchronous data? What if I want to show the last 10 cards in order? Do I send the data over anyway and have client side code queue up 10, and trigger an event that sorts and then control display?

Edit: I found this question which essentially mine, except that I would like to figure out how to design this without relying an synchronous library.

  • 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-27T01:46:43+00:00Added an answer on May 27, 2026 at 1:46 am
    function(err, card_keys) {
        var deck = [];
        for (k in card_keys) {
            // whole bunch of async actions
            dealer.hget(card_keys[k], 'data', function(err, card_data) {
                var deal = eval('(' + card_data + ')');
                deals.push(cards);
            });
        }
        // sync action
        response.json(deals);
    }
    

    You already know response.json fires before the hget are done.

    The answer is reference counting

    function(err, card_keys) {
        var deck = [];
        var count = Object.keys(card_keys).length;
    
        function next() {
            if (--count === 0) {
                response.json(deals.sort(sortThem)); 
            }  
        }
    
        for (k in card_keys) {
            dealer.hget(card_keys[k], 'data', function(err, card_data) {
                var deal = JSON.parse(card_data);
                deals.push(cards);
                next();
            });
        }
    }
    

    You have two other minor problems

    • using eval instead of JSON.parse
    • your deals array is unordered because hget is asynchronous, so you need a sorting function.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
In my XML file chapters tag has more chapter tag.i need to display chapters

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.