0

I work on a script with node.js (v 6.5.0) and the module node-redis that displays a random value from a Redis database then, after a small delay, shows the related value. I'll use this for question/answer drills in different languages. It works for a single question/answer, but I'd like to go further and insert it in a loop.

I tried to use a solution proposed here around Node.js - Maximum call stack size exceeded but can't find my way with this more advance logic and in the end, I get a "Maximum call stack size exceeded". Maybe this technique is not adapted to my problem or I applied things very wrongly. Some directions would be very helpful.

var redis = require('redis');
var wait = require('sleep');
var S = require('string');

var langue = 'EN_'; //In the script it's rather a prompting section.

var count = 0;
var maxCount = 12;

(function drill() {
  if(count < maxCount) {

      (function loop() {

        var client = redis.createClient();

        client.on('connect', function() {
          //console.log('connected');
        });

        client.randomkey(function (err, word) {

          if (S(word).startsWith(langue)) { 
            var chomp = S(word).chompLeft(langue).s;
            console.log(chomp); // Disply word without prefix

            //Wait two seconds before displaying result from Redis
            wait.sleep(2);

            client.zrange(word, 0, -1, function(err, reply) {
              if (err) {
                console.log(err);
              } else {
                console.log(reply);
              }  
            });

            client.quit(); 

          } else {
            //if the random word doesn't match the selected prefix, find another one.
            loop();
          }
          client.quit(); 
        });
      }()); 

  }
  count++;
  drill(); //do it again and again until count = 0

}());
Community
  • 1
  • 1
Plouf
  • 367
  • 7
  • 19
  • At the end of the `drill` function you always call `drill()` again causing the max call stack size to exceed. You might want to move the last two line within the block of the `if(count < maxCount)`. – forrert Sep 12 '16 at 15:37
  • This seems like it could have been achieved better by using a `while` loop instead of recursion.. – Frxstrem Sep 12 '16 at 15:39
  • Thanks! Error corrected. But now I have another trouble: there is no correct sequencing of the key/value pairs. Sometimes it's ok (key --> 2 seconds --> value) but sometimes 4 keys in a row and the values coming later. – Plouf Sep 12 '16 at 15:43

0 Answers0