所以我有一个情况,我有多个承诺链的未知长度。我希望在处理完所有的 CHAINS 后运行一些操作。这有可能吗?这里有一个例子:
app.controller('MainCtrl', function($scope, $q, $timeout) {
var one = $q.defer();
var two = $q.defer();
var three = $q.defer();
var all = $q.all([one.promise, two.promise, three.promise]);
all.then(allSuccess);
function success(data) {
console.log(data);
return data + "Chained";
}
function allSuccess(){
console.log("ALL PROMISES RESOLVED")
}
one.promise.then(success).then(success);
two.promise.then(success);
three.promise.then(success).then(success).then(success);
$timeout(function () {
one.resolve("one done");
}, Math.random() * 1000);
$timeout(function () {
two.resolve("two done");
}, Math.random() * 1000);
$timeout(function () {
three.resolve("three done");
}, Math.random() * 1000);
});
在这个例子中,我为承诺1、2和3设置了一个 $q.all()
,这些承诺将在某个随机时间得到解决。然后在一和三的末尾加上承诺。我希望解决 all
时,所有的链已经解决。下面是运行此代码时的输出:
one done
one doneChained
two done
three done
ALL PROMISES RESOLVED
three doneChained
three doneChainedChained
有没有办法等锁链解开?