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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:41:08+00:00 2026-06-02T12:41:08+00:00

Hope someone can assist with a (simple) async question on node-redis. I’m trying to

  • 0

Hope someone can assist with a (simple) async question on node-redis. I’m trying to load a set from a hash in the redis db and then use that populated set further on. Here’s the code snippet :-

var redis_client = redis.createClient(REDIS_PORT, REDIS_URL); 

redis_client.hgetall(target_hash,function(e,o){

    Object.keys(o).forEach(function(target){

        // get the "name" from the hash         
        redis_client.hget(o[target],"name",function(e,o){
        if (e){
                 console.log("Error occurred getting key: " + e);
              }
        else {
                 redis_client.sadd("newset",o);
             }
    });

});

// the following line prints nothing - why ?? 

redis_client.smembers("newset",redis.print);

When I examine the contents of “newset” in redis it is populated as expected, but at runtime it displayed as empty. I’m sure it’s an async issue – any help much appreciated !

  • 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-02T12:41:10+00:00Added an answer on June 2, 2026 at 12:41 pm

    hgetall is an asynchronous call: when it receives a reply from the redis server, it will eventually call your callback function (target) { ... }. But within your script, it actually returns immediately. Since hgetall returns very fast, Node will immediately run the next statement, smembers. But at this point the sadd statements haven’t run yet (even if your system is very fast because there hasn’t been a context switch yet).

    What you need to do is to make sure smembers isn’t called before all the possible sadd calls have executed. redis_client provides the multi function to allow you to queue up all the sadd calls and run a callback when they’re all done. I haven’t tested this code, but you could try this:

    var redis_client = redis.createClient(REDIS_PORT, REDIS_URL);
    
    redis_client.hgetall(target_hash, function(e, o) {
      var multi = redis_client.multi();
      var keys = Object.keys(o);
      var i = 0;
    
      keys.forEach(function (target) {
        // get the "name" from the hash     
        redis_client.hget(o[target], "name", function(e, o) {
          i++;
          if (e) {
            console.log("Error occurred getting key: " + e);
          } else {
            multi.sadd("newset", o);
          }
    
          if (i == keys.length) {
            multi.exec(function (err, replies) {
              console.log("MULTI got " + replies.length + "replies");
              redis_client.smembers("newset", redis.print);
            });
          }
        });
      });
    });
    

    Some libraries have a built-in equivalent of forEach that allows you to specify a function to be called when the loop is all done. If not, you have to manually keep track of how many callbacks there have been and call smembers after the last one.

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

Sidebar

Related Questions

I hope someone can help, I am trying to enable enable ASP.Net Impersonation under
I hope someone can help with the following. I'm trying to create a user-specific
I hope someone can help me as I've already spent several hours trying to
I hope someone can help me. It's a pretty newbie question, I'm afraid. I
I hope someone can assist me with the problem I'm currently experiencing. We have
Hope someone can help. I have a simple scenario where clicking checkboxes is driving
Hope someone can fix this (hopefully simple) issue I'm having. We've got a Google
I hope someone can point out where I'm going wrong here. I'm trying to
Hope someone can explain what's going on. If I get an object from my
I hope someone here can help me see where this error is coming from.

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.