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

  • Home
  • SEARCH
  • 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 7944907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:41:33+00:00 2026-06-04T00:41:33+00:00

I get some data from a Memcached, but the asynchrony of nodejs completely eludes

  • 0

I get some data from a Memcached, but the asynchrony of nodejs completely eludes me.
I want to put all the results into an object.

This is what I would do normally:

for( x = startX; x <= endX; x++ )
{
  for( y = startY; y <= endY; y++ )
  {
    oData[ x + '_' + y ] = Db.get( x + '_' + y );
  } 
}

But I can’t figure out how

The Db.get() function wants a key and a callback (function(error, result) {})

This would just increment the x…

var onGet = function (error, result)
{
  x++;
  if(!error && result !== null) 
  {
    oData[ x + '_' + y ] = result
  }
};

Db.get(x + '_' + y, onGet);
  • 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-04T00:41:35+00:00Added an answer on June 4, 2026 at 12:41 am

    This is not a recursion problem, it’s an “async problem.” Your issue is that NodeJS access memcached asynchronously and your procedural style code does not. So, you need to think about the problem differently.

    function getMemcacheData(key, data)
    {
        DB.get(key, function(err, result)
        {
            if (err) return false;
    
            data[ key ] = result;
        })
    }
    
    var oData = {};
    
    for (var x = startX; x <= endX; x++)
    {
        for (var y = startY; y <= endY; y++)
        {
            var key = x + "_" + y;
            getMemcacheData(key, oData);
        }
    }
    

    That will work, but – you have another problem then. You have no way of knowing when your MemcacheD data has been loaded into oData, you just have to sit and wait and guess. There are ways to deal with this; but my favorite way is to use a library named async.

    With async, you could do this:

    var syncStack = [],
        oData = {};
    
    for (var x = startX; x <= endX; x++)
    {
        for (var y = startY; y <= endY; y++)
        {
            (function(key)
            {
                syncStack.push(function(callback)
                {
                    DB.get(key, function(err, result)
                    {
                        /** If you don't care about data not being loaded 
                        you do this: */
                        if (!err)
                        {               
                            data[ key ] = result;
                        }
    
                        callback();
    
                        /** If you do care, and you need to terminate if 
                        you don't get all your data, do this: */
                        if (err)
                        {
                            callback(err);
                            return false;
                        }
    
                        data[ key ] = result;
    
                        callback();
                    })                    
                });
            })(x + "_" + y);  
        }
    }
    
    async.parallel(syncStack, function(error)
    {
        //this is where you know that all of your memcached keys have been fetched.
        //do whatever you want here.
    
        //if you chose to use the 2nd method in the fetch call, which does
        //"callback(error)" error here will be whatever you passed as that
        //err argument
    });
    

    What this code is actually doing is creating an array of functions, each of which calls the Db.get method for a specific key, and adds the result to the oData variable.

    After the array of functions is created, we use the async library’s parallel method, which takes an array of functions and calls them all in parallel. This means that your code will send a bunch of requests off to memcached to get your data, all at once. When each function is done, it calls the callback function, which tells the async lib that the request finished. When all of them are finished, async calls the callback closure you supplied as the 2nd argument in the call to the parallel method. When that method is called, you either know A. something went wrong, and you have the error to recover from it or B. all requests are finished, and you may or may not have all the data you requested (expired or stale keys requested, or whatever). From there, you can do whatever you want, knowing you have finished asking for all the keys you needed.

    I hope this helps.

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

Sidebar

Related Questions

I want to get some data from companies' Facebook pages, but I don't know
I am using http://lite.facebook.com And i want to get some data from my account.
I have one problem , I want to get some data from XML file
Im using LINQ over DataSet but just to get data from some tables, by
In our web application we want to get some data from a text file
I'm trying to get some data from my database, and then pass it into
I am trying to get some data from a RSS feed but I am
I want to get some data from html tags in a web page. For
I have a problem, when I want get some data from CSV file to
trying to get some data from a plist into the MWPhotoBrowser sample app. Any

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.