【发布时间】:2017-09-28 15:16:32
【问题描述】:
我正在尝试用我的 Redis 服务器中的内容填充我的 var todos,我知道我必须使用 Promise,但我可能不在正确的位置。
首先,我使用 .smembers() 函数获取所有 id,对于每个 id,我获取具有正确 id 的对象并将其解析为 todos。
var todos=[];
res.locals.redis.smembers("todo:20", function(err, reply){ // i.e. SMEMBERS todo:20 returns 0 and 1
var promises=reply.map(function(elem){
res.locals.redis.get("todo:20:"+elem, function(err, reply1){ // i.e. GET todo:20:0
return new Promise(function(resolve, reject){
todos.push(JSON.parse(reply1));
resolve();
});
});
});
Promise.all(promises)
.then(function(){
res.locals.redis.quit();
res.render('todolist.ejs', {todo: todos});
})
.catch(function(reason){
console.log(reason);
});
});
【问题讨论】:
标签: node.js asynchronous promise node-redis