$ q.all和嵌套的承诺 [英] $q.all and nested promises
本文介绍了$ q.all和嵌套的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个关于使用角$ Q时同步嵌套的承诺的问题。
将以下code确保承诺的整个链条中等待?这意味着将要返回的诺言等待在$ q.all阻止?业务的嵌套调用
Have a question about synchronizing nested promises when using $q in Angular. Will the following code ensure that the entire chain of promises is waited for? Meaning will the nested calls to services that return promises be waited for in the $q.all block?
var call1 = service1.get('/someUr').then(function(){
return service2.get('/someUrl2'); //returns promise
});
var call2 = service3.get('/someUr').then(function(){
return 'hello';
});
var call3 = service4.get('/someUr').then(function(){
return service3.get('/someUrl3');//returns promise
});
$q.all(call1,call2,call3).then(function(){
console.log('All asynch operations are now completed');
});
基本上是:是否有与当前code一个机会,当时的$ q.all所有嵌套之前承诺将执行都解决了?抑或是递归的?
Basically: Is there a chance with the current code that the then of $q.all will execute before all the nested promises are resolved? Or is it recursive?
推荐答案
是的,它看起来像凯文是正确的。我在角创建了一个简单的测试,以及确认行为。
Yes it looks like Kevin is correct. I created a quick test in angular as well to confirm the behavior.
angular.module('myModule').controller('testController', function ($q,$scope) {
function promiseCall(data,timeout) {
var deferred = $q.defer();
setTimeout(function() {
deferred.resolve(data);
console.log(data);
}, timeout);
return deferred.promise;
}
var a = promiseCall('call1 a',1000).then(function(){
return promiseCall('call2 a',50);
});
var b = promiseCall('call1 b',500);
var c = promiseCall('call1 c',1000).then(function(){
return promiseCall('call2 c',50).then(function(){
return promiseCall('call3 c',6000);
});
});
$q.all([a,b,c]).then(function(res){
console.log('all calls are done');
});
});
这篇关于$ q.all和嵌套的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文