In the code below I add functions to an array of promises depending on a variable x
. In the last condition, I need to add the function only after an $http response. Is this code correct? Will the execution of all the promises in $q.all
wait until the $http
function returns with a response?
在下面的代码中,我将函数添加到一个基于变量x的承诺数组中,在最后一个条件中,我需要在$http响应之后添加函数。这段代码是正确的吗?将在$q中执行所有的承诺。所有的等待直到$http函数返回响应?
var promises = [];
array.forEach(function(x){
if (x==1)
promises.push(function1('aaa'));
else if (x==2)
promises.push(function2('bbb'));
else {
$http.get("url.htm").then(function(response) {
promises.push(function3(response));
});
}
});
$q.all(promises).then(function(resultArray) {
// .....
}
1 个解决方案
#1
1
it will not wait for the AJAX response, since its async.
它不会等待AJAX响应,因为它是异步的。
It's not clear what funciton1 & function2 return (promises?), but it should be something more like
目前还不清楚什么是功能和功能返回(承诺?),但它应该是更类似的东西。
var promises = [];
array.forEach(function(x){
if (x===1)
promises.push(function1('aaa'));
else if (x===2)
promises.push(function2('bbb'));
else {
promises.push($http.get("url.htm"))
}
});
$q.all(promises).then(function(resultArray) {
// .....
}
#1
1
it will not wait for the AJAX response, since its async.
它不会等待AJAX响应,因为它是异步的。
It's not clear what funciton1 & function2 return (promises?), but it should be something more like
目前还不清楚什么是功能和功能返回(承诺?),但它应该是更类似的东西。
var promises = [];
array.forEach(function(x){
if (x===1)
promises.push(function1('aaa'));
else if (x===2)
promises.push(function2('bbb'));
else {
promises.push($http.get("url.htm"))
}
});
$q.all(promises).then(function(resultArray) {
// .....
}