javascript - node.js redis and how to use promise when using a module -


i have express route in node server (file required):

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 = [];  // array collect valid hashes     var key;       // check each hash against redisdb use loop     (key in hashes) {         var hash = hashes[key];         console.log("hash " + hash + "  proofed now:");        //now try collect valid hashes in candidates array        if (redisclient.exists(hash) === 1) candidates.push(hash);     }     console.log(json.stringify(candidates)); }); }; 

now here code of module shall manage redis requests:

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

so experience module able console.log results properly. if hash valid, returns true , otherwise false. works expected. problem is, for-loop continuous execution without fetching getting results. think caused race-conditions.

as can see, have started workout there use of q , promise-redis in top of code:

 var promisefactory = require("q").promise,     redis = require('promise-redis')(promisefactory);  this.client = redis.createclient(); 

i know, how make for-loop (in express route) waiting results of redisclient.exists(hash) or in other words, valid hashes candidates array.

please help

like @brad said, use q.all, take array of promises input , return array of results when promises finished:

there mistake in answer:

redis.prototype.exists = function (key) {  return this.client.exists(key)      // changed, still need return promise.     .then(function (reply) {         console.log("reply " + reply);         return (reply);     })     .catch(console.log);  }; 

if understand correctly, want like

exports.init = function () { redis = exports.redis = function () {     var q = require("q"),         promisefactory = 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) {     return this.client.exists(key).then(function (data) {        return data === 1 ? true : false;     }); };  redis.prototype.getactive = function (arry) {     var self = this;     return  q.all(arry.map(self.exists.bind(self))             ).then(function(res){                 return arry.filter(function(val, idx){ return res[idx];});             }); };    return new redis(); }; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -