我正在发出一系列http请求,当结果返回时,我需要将结果归档到list对象中。我用的是棱角分明的承诺。
因为承诺只有在for循环完成后才会解析,所以它们都会被归档到列表的最后一个索引中。
for (var i = 0;i < list.length; i+=1) {
Promise.do(action).then(function(result) {
list[i] //i is always at last index because the for loop has already completed
}
}发布于 2014-08-15 11:47:08
绑定索引作为接收结果的函数的参数:
for (var i = 0;i < list.length; i+=1) {
Promise.do(action).then((function(index, result) {
list[index]
}).bind(null, i));
}https://stackoverflow.com/questions/25325037
复制相似问题