node.js redis 以及使用模块时如何使用promise

gwi*_*ger 5 javascript redis node.js promise q

我在节点服务器中有一个这样的 Express 路由(需要文件):

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));
});
};
Run Code Online (Sandbox Code Playgroud)

现在这是我的模块的代码,它将管理所有 redis 请求:

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();
};
Run Code Online (Sandbox Code Playgroud)

所以我的经验是该模块能够正确地 console.log 结果。如果散列有效,则返回真,否则返回假。这按预期工作。问题是,for 循环在不获取结果的情况下继续执行。我认为这是由竞争条件引起的。

正如你所看到的,我已经开始在我的代码顶部使用 Q 和 promise-redis 来锻炼一些东西:

 var promiseFactory = require("q").Promise,
    redis = require('promise-redis')(promiseFactory);

this.client = redis.createClient();
Run Code Online (Sandbox Code Playgroud)

我想知道,我如何让我的 for 循环(在 Express 路线中)等待 redisClient.exists(hash) 的结果,或者换句话说,将所有有效的哈希值放入我的候选数组中。

请帮忙

mid*_*ido 1

就像@brad所说,你可以使用Q.all,它将接受一组承诺作为输入,然后在所有承诺完成时返回一组结果:

\n\n

你的回答有一个错误:

\n\n
Redis.prototype.exists = function (key) {\n\nreturn this.client.exists(key)      // CHANGED, you still need to return a promise.\n    .then(function (reply) {\n        console.log("reply " + reply);\n        return (reply);\n    })\n    .catch(console.log);\n\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我理解正确的话,你想要的是类似的东西

\n\n
exports.init = function () {\nRedis = exports.Redis = function () {\n    var Q = require("q"),\n        promiseFactory = Q.Promise,\n        redis = require(\'promise-redis\')(promiseFactory);\n\n    this.client = redis.createClient();\n    this.client.on(\'error\', function (err) {\n        console.log(\'redis error \xe2\x80\x93 \' + client.host + \':\' + client.port + \' \xe2\x80\x93 \' + err);\n    });\n\nRedis.prototype.exists = function (key) {\n    return this.client.exists(key).then(function (data) {\n       return data === 1 ? true : false;\n    });\n};\n\nRedis.prototype.getActive = function (arry) {\n    var self = this;\n    return  Q.all(arry.map(self.exists.bind(self))\n            ).then(function(res){\n                return arry.filter(function(val, idx){ return res[idx];});\n            });\n};\n\n\n\nreturn new Redis();\n};\n
Run Code Online (Sandbox Code Playgroud)\n