【发布时间】:2016-08-22 16:20:03
【问题描述】:
我在 JavaScript 中有以下代码:
for (var i = 0; i< uuids.length; i++){
var attr = uuids[i]
var uuid = attr["uuid"];
console.log("uuid is: " + uuid)
multiClient.hget(var1, var2, function(err, res1){
console.log("uuid INSIDE hget is: " + uuid)
}
}
hget 是一种异步方法。下面是这个函数的打印结果:
"uuid is: 1"
"uuid is: 2"
"uuid INSIDE hget is: 2"
"uuid INSIDE hget is: 2"
我希望在 hget 函数中保存 uuid 的上下文,所以我会得到:
"uuid is: 1"
"uuid is: 2"
"uuid INSIDE hget is: 1" (where all the context before the loop has saved for this uuid)
"uuid INSIDE hget is: 2" (where all the context before the loop has saved for this uuid)
我该怎么做?
【问题讨论】:
标签: javascript