我想填充我的var todos
与我在我的Redis服务器,我得到,我必须使用承诺,但我可能不是在正确的地方。使用承诺和redis回调
首先,我得到所有ID为.smembers()
函数,并为每个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);
});
});
这promisify功能改变加载速度?我得到它看起来更好,但我仍然困惑为什么我会比以前的答案更好 –
答案是**否**,_promisify_函数不会影响性能。正如你在例子中看到的,它返回一个简单的逻辑对象:'拒绝'承诺,当有错误,或'解析'异步调用的结果,否则。 – alexmac