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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:57:42+00:00 2026-06-03T06:57:42+00:00

I really want to like node.js with redis, but I can’t conquer asynchronicity. Again

  • 0

I really want to like node.js with redis, but I can’t conquer asynchronicity. Again I have what is a simple task in a traditional database and language. My question is more about accomplishing control flow and logic in asynchronous database fetching than whether my problem-solving approach is optimal or not.

Here’s what I’m trying to do: I have redis keys made up of words, let’s just say car and card. Now, given an input string, I want to know what the longest substring is, that matches a key in redis. I only need to check substrings starting from position 0 of the given string, so the complexity is low.

Example: cardinal has the key card in it, and also car, but card is longer. Cape does not match either key.

My approach is: start with the whole string and check if it matches a key. If yes, return that key. Otherwise, repeat the same process with the string minus the last character.

How can I accomplish this task? Different approaches are welcome.

I know a little about the async library and it looks like waterfall is best for what I’m doing. However, it appears that I need to type all the functions from string.length, string.length-1, etc. until the last single character. What I’m looking for is a good replacement for a for loop with a break.

Below I test with an input I assume is always 3 characters or more (because it’s already ugly and more nesting seems pointless for testing). It works, carde resulting in card, and care -> car. Nonsense gives no match.

var http = require("http");
var redis = require("redis");

http.createServer(function(request, response) {
    client = redis.createClient();
    word = "carde";
    client.keys(word, function(err, reply) {
        if(err) { response.end(err); client.end(); }
        else {
          if(reply.length > 0) {
            response.end(word);
            client.end();
          }
          else {
            client.keys(word.slice(0,-1), function(err, reply) {
                if(err) { response.end(err); client.end(); }
                else {
                  if(reply.length > 0) {
                    response.end(word.slice(0, -1));
                    client.end();
                  }
                  else {
                    client.keys(word.slice(0, -2), function(err,reply) {
                        if(err) { response.end(err); client.end(); }
                        else {
                          if(reply.length > 0) {
                            response.end(word.slice(0, -2));
                            client.end();
                          }
                          else {
                            response.end("no match");
                          }
                        }
                      });
                  }
                }
              });
          }
        }
    });
  }).listen(8000);

I also tried recursion and it might be the best method. (Thanks to Timonthy Strimple for correcting a mistake).

http.createServer(function(request, response) {
    client = redis.createClient();
    recursiveKeys("cardinalsin", client, response);
  }).listen(8000);

function recursiveKeys(word, client, response) {
  if (word.length == 0) {
    response.write("0");
    response.end();
    client.end();
  }
  else {
    client.keys(word, function(err, reply) {
        if(err) {
          response.write("0");
          response.end();
          client.end();
        }
        else {
          if(reply.length > 0) {
            response.write(word);
            response.end();
            client.end();
          }
          else {
            return recursiveKeys(word.slice(0,-1), client, response);
          }
        }
      });
  }
}
  • 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-03T06:57:43+00:00Added an answer on June 3, 2026 at 6:57 am

    I agree that a recursive solution is probably the best. I approached this problem before studying your code (so as to not affect the experiment) and arrived at a very similar solution. Search for a word with http://localhost:3000/?word=cardinal

    var http  = require('http');
    var redis = require('redis');
    var url   = require('url');
    
    var findWord = function(needle, client, callback) {
      if (needle.length <= 0) { return callback("Word not found") }
    
      client.keys(needle, function(err, reply) {
        if (err) { callback("Word not found"); }
        else {
          if (reply.length > 0) {
            callback(needle);
          } else {
            findWord(needle.slice(0, -1), client, callback);
          }
        }
      });
    };
    
    var server = http.createServer(function(request, response) {
      var query  = url.parse(request.url, true).query;
      var word   = query.word || "";
      var client = redis.createClient();
    
      findWord(word, client, function(found) {
        client.end();
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.end(found);
      });
    });
    server.listen(3000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really just want to do something like x <- as.integer(c(1,2,3)) But because c(1,2,3)
I would really want to have a Regex that is executable in node.js (so
i want to fetch information from the database using objects. i really like this
I really want to have a column of inifinite length for one of the
I know about the ADODB.Stream object . But what I really want is a
Say that I have two html nodes like this: <div class='node a'>one</div> <input class='node
I have posted this to ServerFault, but the Node.js community seems tiny there, so
I have started a bounty for this question ...because I really want the community's
I am not sure if this question is not silly, but I really want
I have 3 tables which look like this: Location Node Sektor ----- ------- -------

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.