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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:41:47+00:00 2026-05-26T13:41:47+00:00

Please could I ask for some advice on a control flow issue with node

  • 0

Please could I ask for some advice on a control flow issue with node and redis? (aka Python coder trying to get used to JavaScript)

I don’t understand why client.smembers and client.get (Redis lookups) need to be callbacks rather than simply being statements – it makes life very complicated.

Basically I’d like to query a set, and then when I have the results for the set, I need to carry out a get for each result. When I’ve got all the data, I need to broadcast it back to the client.

Currently I do this inside two callbacks, using a global object, which seems messy. I’m not even sure if it’s safe (will the code wait for one client.get to complete before starting another?).

The current code looks like this:

var all_users = [];
// Get all the users for this page.
client.smembers("page:" + current_page_id, function (err, user_ids ) {
  // Now get the name of each of those users.
  for (var i = 0; i < user_ids.length; i++) {
     client.get('user:' + user_ids[i] + ':name', function(err, name) {
       var myobj = {};
       myobj[user_ids[i]] = name;
       all_users.push(myobj);  
       // Broadcast when we have got to the end of the loop, 
       // so all users have been added to the list - 
       // is this the best way? It seems messy.  
       if (i === (user_ids.length - 1)) {
           socket.broadcast('all_users', all_users); 
       }
     });       
   }
 });

But this seems very messy. Is it really the best way to do this? How can I be sure that all lookups have been performed before calling socket.broadcast?

scratches head Thanks in advance for any advice.

  • 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-26T13:41:47+00:00Added an answer on May 26, 2026 at 1:41 pm

    I don’t understand why client.smembers and client.get (Redis lookups) need to be callbacks rather than simply being statements – it makes life very complicated.

    That’s what Node is. (I’m pretty sure that this topic was discussed more than enough times here, look through other questions, it’s definitely there)

    How can I be sure that all lookups have been performed before calling socket.broadcast?

    That’s what is err for in callback function. This is kinda Node’s standard – first parameter in callback is error object (null if everything fine). So just use something like this to be sure no errors occurred:

    if (err) {
      ...    // handle errors.
      return // or not, it depends.
    }
    
    ... // process results
    

    But this seems very messy.

    You’ll get used to it. I’m actually finding it nice, when code is well formatted and project is cleverly structured.

    Other ways are:

    • Using libraries to control async code-flow (Async.js, Step.js, etc.)
    • If spaghetti-style code is what you think mess is, define some functions to process results and pass them as parameters instead of anonymous ones.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please demystify interfaces for me or point me to some good examples?
Before I ask my question can I please ask not to get a lecture
Please someone could fix that question for me? don't know how to ask =/
Please could someone post an example of how to check if an element exists
Ive decided that I really dont like microsoft and their ways. Please could you
Could somebody please do a rundown of how to programmatically encrypt a config-file in
Could somebody please name a few. I could given time, but this is for
Could you please show me the C# Equivalent of this VB.NET code: Public Partial
Could you please explain what the practical usage is for the internal keyword in
Could somebody please point me in the right direction for learning how to do

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.