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
}());