Javascript AngularJS$q.all-在http调用之间等待
所以我遇到了一种情况,我需要执行一系列http调用,然后一旦它们完成,继续执行流程中的下一步 下面是执行此操作并运行良好的代码 但是,我现在需要在每次http调用之间等待几秒钟。有没有一种方法可以通过我当前的设置来传递一个超时,或者它会涉及到一些重构 如果需要,可以发布更多代码。我已经尝试将timeout-config变量传递到http调用中,但是,它们仍然会同时被触发 任何建议都很好 代码Javascript AngularJS$q.all-在http调用之间等待,javascript,angularjs,http,settimeout,Javascript,Angularjs,Http,Settimeout,所以我遇到了一种情况,我需要执行一系列http调用,然后一旦它们完成,继续执行流程中的下一步 下面是执行此操作并运行良好的代码 但是,我现在需要在每次http调用之间等待几秒钟。有没有一种方法可以通过我当前的设置来传递一个超时,或者它会涉及到一些重构 如果需要,可以发布更多代码。我已经尝试将timeout-config变量传递到http调用中,但是,它们仍然会同时被触发 任何建议都很好 代码 您可能不想使用$q.all,而是希望在上一次调用成功时执行顺序调用,可能需要使用$timeout。也许你
您可能不想使用
$q.all
,而是希望在上一次调用成功时执行顺序调用,可能需要使用$timeout
。也许你可以构建一个递归函数
像这样的
function performSequentialCalls (index) {
if(angular.isUndefined(array[index])) {
return;
}
getFile(array[index].id).then(function() {
$timeout(function() {
performSequentialCalls(index + 1)
}, 1000) // waiting 1 sec after each call
})
}
正确注入所需的物质。这假定array
包含具有id
s的对象,您可以使用这些对象执行API调用。还假设您正在使用$http
。如果使用$resource
,则相应地添加$promise
希望对你有所帮助
function getItemsWithDelay(index) {
getFile(object[index].id).then(()=>{
setTimeout(()=>{
if(index+1 > object.length) { return }
getItemsWithDelay(index+1)
}, 5000)
})
}
你可以连续打电话这是一个很棒的技巧性问题,在面试中被问到,不管怎样,我有一个类似的要求,在互联网上做了一些研究,感谢你的推荐 我能够延迟angularjs中的所有promise调用,这同样可以应用于正常的JS语法 我需要将任务发送到TTPAPI,他们要求在每个调用中添加延迟
_sendTasks: function(taskMeta) {
var defer = $q.defer();
var promiseArray = [];
const delayIncrement = 1000 * 5;
let delay = 0;
for (i = 0; i < taskMeta.length; i++) {
// using 'let' keyword is VERY IMPORTANT else 'var' will send the same task in all http calls
let requestTask = {
"action": "SOME_ACTION",
"userId": '',
"sessionId": '',
};
// new Promise can be replaced with $q - you can try that, I haven't test it although.
promiseArray.push(new Promise(() => setTimeout(() => $http.post(config.API_ROOT_URL + '/' + requestTask.action, requestTask), delay)));
delay += delayIncrement;
}
$q.all(promiseArray).
then(function(results) {
// handle the results and resolve it at the end
defer.resolve(allResponses);
})
.catch(error => {
console.log(error);
defer.reject("failed to execute");
});
return defer.promise;
}
\u发送任务:函数(taskMeta){
var defer=$q.defer();
var promiseArray=[];
常数延迟增量=1000*5;
设延迟=0;
对于(i=0;isetTimeout(()=>http.post(config.API_ROOT_URL+'/'+requestTask.action,requestTask),delay));
延迟+=延迟增量;
}
$q.all(promiseArray)。
然后(函数(结果){
//处理结果并在最后解决它
延迟。解决(所有响应);
})
.catch(错误=>{
console.log(错误);
延迟、拒绝(“未能执行”);
});
回报、承诺;
}
注意:在FOR循环中使用'let'关键字非常重要,否则'var'将在所有http调用中发送相同的任务-由于关闭/上下文切换鉴于我的上述设置,我将如何调用它?干杯!鉴于我的上述设置,我将如何调用该函数?@user2085143您可以在希望启动API调用的位置调用该函数。而且,如果您看到
如果条件带有angular.isUndefined…
,您可以在那里编写一些代码(在返回之前
),这些代码需要在所有调用完成时执行。感谢mil。一旦我有了合适的时间,我会尝试一下,如果它有效的话,我会把它标记为正确的。
_sendTasks: function(taskMeta) {
var defer = $q.defer();
var promiseArray = [];
const delayIncrement = 1000 * 5;
let delay = 0;
for (i = 0; i < taskMeta.length; i++) {
// using 'let' keyword is VERY IMPORTANT else 'var' will send the same task in all http calls
let requestTask = {
"action": "SOME_ACTION",
"userId": '',
"sessionId": '',
};
// new Promise can be replaced with $q - you can try that, I haven't test it although.
promiseArray.push(new Promise(() => setTimeout(() => $http.post(config.API_ROOT_URL + '/' + requestTask.action, requestTask), delay)));
delay += delayIncrement;
}
$q.all(promiseArray).
then(function(results) {
// handle the results and resolve it at the end
defer.resolve(allResponses);
})
.catch(error => {
console.log(error);
defer.reject("failed to execute");
});
return defer.promise;
}