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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:55:05+00:00 2026-06-12T18:55:05+00:00

I would like to ask this question, because I’m not sure if I got

  • 0

I would like to ask this question, because I’m not sure if I got the Node.js logic right

I have a set of id’s that I need to query using redis’ get method. And after checking a certain value(let’s say that im checking whether the object I get with given “key” has a null name), I add them to a list. Here is my example code;

var finalList = [];
var list = [];
redisClient.smembers("student_list", function(err,result){
            list = result; //id's of students
            console.log(result);

            var possibleStudents = [];


            for(var i = 0; i < list.length; i++){


                redisClient.get(list[i], function(err, result){
                    if(err)
                        console.log("Error: "+err);
                    else{
                        tempObject = JSON.parse(result);
                        if(tempObject.name != null){
                            finalList.push(tempObject);
                        }
                    }
                });     
            }

    });
   console.log("Goes here after checking every single object");

But as expected due to the async nature of Node, without checking every single id in the list it executes the “Goes here…”. My need is to apply the rest of procedures after every id is checked(mapping in redis db and checking the name). But I do not know how to do it. Maybe if I can attach callback to the for loop and assure the rest of my functions start to run after loop is finished(i know it’s impossible but just to give an idea)?

  • 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-12T18:55:06+00:00Added an answer on June 12, 2026 at 6:55 pm

    I would go the route you suggest in your question and attach a custom callback to your fetching function:

    function getStudentsData(callback) {
        var setList = [];
        var dataList = [];
    
        redisClient.smembers("student_setList", function(err,result) {
            setList = result; //id's of students
    
            for(var i = 0; i < setList.length; i++) {
                redisClient.get(setList[i], function(err, result) {
                    if(err) {
                        console.log("Error: "+err);
                    } else {
                        tempObject = JSON.parse(result);
                        if(tempObject.name != null) {
                            dataList.push(tempObject);
                        }
                    }
                });     
            }
    
            if(dataList.length == setList.length) {
                if(typeof callback == "function") {
                    callback(dataList);
                }
                console.log("getStudentsData: done");
            } else {
                console.log("getStudentsData: length mistmach");
            }
    
        });
    }
    
    getStudentsData(function(dataList) {
        console.log("Goes here after checking every single object");
        console.log(dataList.length);
        //More code here
    });
    

    That’s probably the most efficient method; alternatively you could rely on an old school while loop until the data is ready:

    var finalList = [];
    var list = [0];
    
    redisClient.smembers("student_list", function(err,result) {
        list = result; //id's of students
        var possibleStudents = [];
    
        for(var i = 0; i < list.length; i++) {
            redisClient.get(list[i], function(err, result) {
                if(err) {
                    console.log("Error: "+err);
                } else {
                    tempObject = JSON.parse(result);
                    if(tempObject.name != null) {
                        finalList.push(tempObject);
                    }
                }
            });     
        }
    });
    
    
    process.nextTick(function() {
        if(finalList.length == list.length) {
            //Done
            console.log("Goes here after checking every single object");
            console.log(dataList.length);
            //More code here
        } else {
            //Not done, keep looping
            process.nextTick(arguments.callee);
        }
    });
    

    We use process.nextTick instead of an actual while to make sure other requests are not blocked in the meantime; due to the single threaded nature of Javascript this is the preferred way. I’m throwing this in for the sake of completeness, but the former method is more efficient and fits better with node.js, so go for it unless a major rewrite is involved.

    It’s worth nothing that both cases rely on async callbacks, which means any code outside it can still potentially run before others are done. E.g., using our first snippet:

    function getStudentsData(callback) {
        //[...]
    }
    
    getStudentsData(function(dataList) {
        //[...]
    });
    
    console.log("hello world");
    

    That last console.log is almost guaranteed to run before our callback passed to getStudentsData gets fired. Workaround? Design for it, it’s just how node.js works. In our case above it’s easy, we just would call console.log only in our callback passed to getStudentsData and not outside it. Other scenarios require solutions that depart a bit more from traditional procedural coding, but once you get your head around it you’ll find being event-driven and non-blocking is actually a pretty powerful feature.

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

Sidebar

Related Questions

I would like to ask you this question because I am a bit stuck
I would like to ask this question to those people who have an experience
Not really sure how to ask this question because I really don't know what
this is my first question in here, and I would like to ask if
I know this is a weird question to ask, but I would like to
I hope this is the right place to ask this. I would like to
I would like to ask such question, I have XML xsd`s, which generate beans
I'd like to ask a follow-up question to this issue , please, because an
The title of this Question may not be accurate because I wasn't sure how
I'm embarrassed to even ask this question, but not sure of the syntax or

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.