我有一组异步函数,如果前一个函数已经解决,那么运行一个函数才有意义。您可以将它们视为对不同 URL 的 HTT 获取请求,例如
$http.get('/step1')
$http.get('/step2')
$http.get('/step3')
$http.get('/step4')
我怎样才能序列化它们?
编辑:数组中有 N 个。所以我不能明确地展开它们并用'then'加入它们,例如:
var calls = Array()
for(var i = 0; i < N; i++)
{
var d = $q.defer();
...
calls.push(d.promise);
}
..
// How to do resolve the elements of 'calls' in order?
编辑2:
我想:
Running Step #0
Step completed: #0 OK
Running Step #1
Step completed: #1 OK
Running Step #2
Step completed: #2 OK
Running Step #3
Step completed: #3 OK
Running Step #4
Step completed: #4 OK
Running Step #5
Step completed: #5 OK
不是
Running Step #0
Running Step #1
Running Step #2
Running Step #3
Running Step #4
Running Step #5
Step completed: #0 OK
Step completed: #1 OK
Step completed: #2 OK
Step completed: #3 OK
Step completed: #4 OK
Step completed: #5 OK