我有以下链接的 promise 序列:
$scope.promisesInProgress = true
myService.myFirstPromise(id)
.then(function(data){
$scope.firstResponse = data;
return myService.mySecondPromise(id);
})
.then(function(data){
$scope.secondResponse = data;
})
.finally(function(){
$scope.promisesInProgress = false;
});
是
finally()无论前两个 promise 的成功/失败如何,最后都会调用回调函数?
例如,如果
myFirstPromise()返回 400 响应,
mySecondPromise()永远不会被调用 - 但我认为
finally()块仍然会被抛出?如果
mySecondPromise() 也是如此返回 400(并且
$scope.secondResponse 从未设置)并且如果两个 promise 都返回 200。
请您参考如下方法:
Angular 1.x $q受 Kris Kowal 的 Q 启发的服务,基于 docs :
finally(callback, notifyCallback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.
所以是的,不管
myFirstPromise解决或拒绝,
finally()块将始终被调用
更新,
需要注意的是,
finally()
myFirstPromise块会在
mySecondPromise 之前被调用已解决(或拒绝),因为
myFirstPromise和
mySecondPromise是不同的 promise 实例,和
mySecondPromise在
myFirstPromise 之后创建的 promise 实例解决




