title: node.js redis and how to use promise when using a module
body: <p>I have an Express route like this in an node server (file is required):</p>
  
  <pre><code>var redis = require('../modules/redis');
  
  module.exports = function (app) {
  
  var redisClient = redis.init();
  
  
  app.post('/auth/ticket', cors(), function (req, res) {
  
  
      var hashes = ['hash1','hash2', 'hash3'];
  
      var candidates = [];  // An array to collect valid hashes
      var key;  
      // to check each hash against a RedisDB I use a For Loop
      for (key in hashes) {
          var hash = hashes[key];
          console.log("Hash " + hash + "  will be proofed now:");
         //now I try to collect the valid hashes in the candidates array
         if (redisClient.exists(hash) === 1) candidates.push(hash);
      }
      console.log(JSON.stringify(candidates));
  });
  };
  </code></pre>
  
  <p>Now here is the code of my module which shall manage all the redis requests:</p>
  
  <pre><code>exports.init = function () {
  Redis = exports.Redis = function () {
      var promiseFactory = require("q").Promise,
          redis = require('promise-redis')(promiseFactory);
  
      this.client = redis.createClient();
      this.client.on('error', function (err) {
          console.log('redis error – ' + client.host + ':' + client.port + ' – ' + err);
      });
  
  Redis.prototype.exists = function (key) {
      this.client.exists(key, function (err, data) {
         return data === 1 ? true : false;
      });
  };
  
  return new Redis();
  };
  </code></pre>
  
  <p>So what I experience is that the module is able to console.log the results properly. If a hash is valid, it returns true and otherwise false. This works as expected. 
  Problem is, that the for-loop continuous the execution without fetching getting the results. I think this is caused by race-conditions.</p>
  
  <p>As you can see, I have started to workout something there with the use of Q and promise-redis in the top of my code:</p>
  
  <pre><code> var promiseFactory = require("q").Promise,
      redis = require('promise-redis')(promiseFactory);
  
  this.client = redis.createClient();
  </code></pre>
  
  <p>I like to know, how I make my for-loop (in the Express route) waiting for the results of redisClient.exists(hash) or in other words, to get all valid hashes into my candidates array.</p>
  
  <p>Please help</p>