我正在分离两个系列的步骤,每条腿有两个独立的异步动作块:
Time Threads forking and joining:
| *
| / \
V / \
+-----+ +-----+
/ \
/ \
$http.get('/foo') $http.get('/baz')
| |
V V
$http.get('/bar') $http.get('/blap') <-- Problem children
| |
V V
\ /
\ /
+------+ +------+
\ /
\ /
+
|
$log.log("Carry on")
是否可以在&#34;问题子级&#34;中使用$q.defer()
进行上述分叉/加入?
这就是我用$q.defer()
var left = $q.defer()
var right = $q.defer()
$http.get('/foo')
.success(function() {
$http.get('/bar')
.success(function() {
left.resolve()
})
})
$http.get('/baz')
.success(function() {
$http.get('/blap')
.success(function() {
right.resolve()
})
})
$q.all([left.promise,right.promise])
.then(function() {
$log.log("Carry on")
})
答案 0 :(得分:3)
你可以在then函数中返回promises来链接它们
var promise1 = $http.get('/foo')
.then(function() {
return $http.get('/bar');
})
var promise2 = $http.get('/baz')
.then(function() {
return $http.get('/blap');
});
$q.all([promise1,promise2]).then(function(results){
var promise1Result = results[0]; // result of /bar
var promise2Result = results[1]; // result of /blap
// .... and do stuff
});