【发布时间】:2016-11-28 01:21:11
【问题描述】:
我一直在通过this link研究promise,我理解它的想法
var parentID;
$http.get('/api/user/name')
.then(function(response) {
parentID = response.data['ID'];
for (var i = 0; i < response.data['event-types'].length; i++) {
return $http.get('/api/security/' + response.data['event-types'][i]['key']);
}
})
.then(function(response) {
// response only returns one result of the many promises from the for loop
// do something with parentID;
});
但是,我的用例需要循环并发送创建多个承诺。我试图链接上面的示例,但只执行了从 for 循环创建的唯一一个承诺。
如何在继续访问变量 parentID 的同时继续链接所有承诺?
【问题讨论】:
-
第一个
.then处理程序不返回promise.. 可能是q.all会有所帮助! -
在一个函数中只能执行一个返回,所以基本上你的循环在第一次返回时会中断
标签: javascript angularjs