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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:45:23+00:00 2026-05-28T15:45:23+00:00

Since Node is asynchronous, I’m having problems trying to get a callback to return

  • 0

Since Node is asynchronous, I’m having problems trying to get a callback to return values to me properly.

I’ve tried the following:

var libUser = {
    lookupUser: {},
    getName: function(userID) {
        // If it's in our cache, just return it, else find it, then cache it.
        if('userName_' + userID in this.lookupUser) {
            return this.lookupUser['userName_' + userID];
        }else{
            // Lookup the table
            var userName;
            this.tableLookup(["agent_name"], "_login_", " WHERE agent_id = " + userID, function(d) {
                userName = d[0].agent_name;
            });

            this.lookupUser['userName_' + userID] = userName; // Add to cache

            return userName;
        }
    },
    tableLookup: function(fields, table, clauses, cb) {
        var query = "SELECT " + fields.join(", ") + " FROM " + table + " " + clauses;
        client.query(query, function(err, results) {
            if(err) console.log(err.error);

            cb(results);
        });
    }
};

However, obviously due to race conditions, the userName variable is never set by the callback from this.tableLookup.

So how can I get this value back?

  • 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-28T15:45:24+00:00Added an answer on May 28, 2026 at 3:45 pm
     var userName; // user name is undefined
     this.tableLookup(["agent_name"], "_login_", " WHERE agent_id = " + userID, function(d) {
         // this will run later
         userName = d[0].agent_name;
     });
    
     // username still undefined
     return userName;
    

    So fix your code

    getName: function(userID, cb) {
        var that = this;
        // If it's in our cache, just return it, else find it, then cache it.
        if ('userName_' + userID in this.lookupUser) {
            cb(this.lookupUser['userName_' + userID]);
        } else {
            // Lookup the table
            var userName;
            this.tableLookup(["agent_name"], "_login_", " WHERE agent_id = " + userID, function(d) {
                userName = d[0].agent_name;
    
                that.lookupUser['userName_' + userID] = userName; // Add to cache
                cb(userName);
            });
        }
    },
    

    and use

    libUser.getName(name, function (user) {
      // do something
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my head around asynchronous programming in C# 5 and the
I'm getting the following error when trying to return the value using the operator*()
I am having problems with my node.js web server. My server is listening on
Wikipedia: Directed Acyclic Graph Not sure if leaf node is still proper terminology since
Since I've known about Node.js, I've always been a fan of it. But today
I'm a newbie both on event/callback style programming and NodeJS. I'm trying to implement
I'm running a distributed erlang system with one node per machine. Since DNS is
Since node is basically a single process, when something goes terribly wrong, the whole
Is there a chance to somehow redirect www to non-www URLs in node.js? Since
Since many node.js scripts follow a pattern of doing something asynchronously (example below), how

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.