Javascript 在循环中调用API-角度js
我有一套API,必须在for循环中运行Javascript 在循环中调用API-角度js,javascript,angularjs,api,for-loop,promise,Javascript,Angularjs,Api,For Loop,Promise,我有一套API,必须在for循环中运行 for(var i=0; i<array.length; i++ ) service.getfunction(array[i]).then(function(response) { service.getfunction1(response).then(function(response) { service.getfunction2(response).then(function(response) {
for(var i=0; i<array.length; i++ )
service.getfunction(array[i]).then(function(response) {
service.getfunction1(response).then(function(response) {
service.getfunction2(response).then(function(response) {
service.getfunction3(response).then(function(response) {
console.log(response);
});
});
});
});
)
for(var i=0;i将其包装在函数中:
// Declare function
function LoopAPI() {
for(var i=0; i<array.length; i++ )
service.getfunction(array[i]).then(function(response) {
service.getfunction1(response).then(function(response) {
service.getfunction2(response).then(function(response) {
service.getfunction3(response).then(function(response) {
console.log(response);
// Call function again
LoopAPI();
});
});
});
});
)
}
// First call
LoopAPI();
//声明函数
函数LoopAPI(){
对于(var i=0;i将其包装在函数中:
// Declare function
function LoopAPI() {
for(var i=0; i<array.length; i++ )
service.getfunction(array[i]).then(function(response) {
service.getfunction1(response).then(function(response) {
service.getfunction2(response).then(function(response) {
service.getfunction3(response).then(function(response) {
console.log(response);
// Call function again
LoopAPI();
});
});
});
});
)
}
// First call
LoopAPI();
//声明函数
函数LoopAPI(){
对于(var i=0;i首先,您可以像以下那样将您的承诺链接起来:
function doJob(i) {
return service.getfunction(array[i]).then(function(response) {
return service.getfunction1(response);
}).then(function(response) {
return service.getfunction2(response);
}).then(function(response) {
return service.getfunction3(response);
}).then(function(response) {
console.log(response);
});
}
此函数将返回承诺,在所有服务调用完成后将解决该承诺。
现在让我们使用它:
var p = Promise.resolve(true);
for(var i = 0; i < array.length; i++) {
(function(idx) { //We need to wrap it or we will pass incorrect i into doJob
p = p.then(function() {
return doJob(idx);
});
})(i);
}
p.then(function() {
console.log('Everything done');
});
var p=Promise.resolve(真);
对于(var i=0;i
首先,您可以将您的承诺链接为:
function doJob(i) {
return service.getfunction(array[i]).then(function(response) {
return service.getfunction1(response);
}).then(function(response) {
return service.getfunction2(response);
}).then(function(response) {
return service.getfunction3(response);
}).then(function(response) {
console.log(response);
});
}
此函数将返回承诺,在所有服务调用完成后将解决该承诺。
现在让我们使用它:
var p = Promise.resolve(true);
for(var i = 0; i < array.length; i++) {
(function(idx) { //We need to wrap it or we will pass incorrect i into doJob
p = p.then(function() {
return doJob(idx);
});
})(i);
}
p.then(function() {
console.log('Everything done');
});
var p=Promise.resolve(真);
对于(var i=0;i
您可以链接承诺,然后使用Array.reduce按顺序调用它们:
array.reduce(function(previous, next)
{
return previous.then(function()
{
return service.getfunction(next);
})
.then(function(response)
{
return service.getfunction1(response);
})
.then(function(response)
{
return service.getfunction2(response);
})
.then(function(response)
{
return service.getfunction3(response);
});
}, Promise.resolve())
.then(function()
{
console.log("All done.");
})
.catch(function(error)
{
console.log(error);
});
您可以使用Array.reduce按如下顺序调用承诺:
array.reduce(function(previous, next)
{
return previous.then(function()
{
return service.getfunction(next);
})
.then(function(response)
{
return service.getfunction1(response);
})
.then(function(response)
{
return service.getfunction2(response);
})
.then(function(response)
{
return service.getfunction3(response);
});
}, Promise.resolve())
.then(function()
{
console.log("All done.");
})
.catch(function(error)
{
console.log(error);
});
这可以通过省略返回service.getfunctionX(response)
来简化。默认情况下,then
将返回前面承诺中解析的值,因此您只需编写service.getFunction(数组[i])。然后(service.getfunction1)。然后(service.getfunction2)。然后(service.getfunction3)
…如果任何API失败了怎么办?@Anna您可以使用then
函数的第二个参数来处理错误,或者使用chaincatch
函数。它将在最后一次catch
之后从整个链中获取所有错误。省略返回服务可以大大简化此过程。getfunctionX(response)
。then
默认情况下将返回前面承诺中解析的值,因此您只需编写service.getFunction(数组[i])。然后(service.getfunction1)。然后(service.getfunction2)。然后(service.getfunction3)
…如果任何API失败怎么办?@Anna您可以使用then
函数的第二个参数来处理错误,或者使用chaincatch
函数。它将在最后一次catch
之后从整个链中获取所有错误。