我在这里使用Bluebird
Promise。请注意,代码的意图非常清晰并且没有嵌套。
首先,让我们讲解 hgetall调用和客户端-
var client = Promise.promisifyAll(client);
现在,让我们用promise编写代码,
.then而不是用进行节点回调和聚合
.map。什么
.then确实是信号的异步操作完成。
.map接受一系列事物并将它们全部映射到异步操作,就像您的hgetall调用一样。
请注意,Bluebird如何(默认情况下)向
Async后缀方法添加后缀。
exports.awesomeThings = function(req, res) { // make initial request, map the array - each element to a result return client.lrangeAsync("awesomeThings", 0, -1).map(function(awesomeThing) { return client.hgetallAsync("awesomething:" + awesomeThing); }).then(function(things){ // all results ready console.log(things); // log them res.send(JSON.stringify(things)); // send them return things; // so you can use from outside });};


