如何检查 angular-promise 的状态?
How to check the state of an angular-promise?
在我的网站上,我有 API 用于 twitter 和 facebook,它启用 "mentions" 功能(每当我们使用 @ 符号时弹出的功能)
但是,某些功能的访问令牌通常会过期,导致 API 无法正常工作。我将所有 API 存储在一个数组中,然后我需要检查令牌是否失败或未导致解决或拒绝 API 承诺。
这是一个旧代码,由于 $q.all 需要更改。由于 $q.all 在所有承诺都已解决时起作用,从而触发 .then() 调用,这导致 .then() 函数在我的情况下永远不起作用(因为 Facebook API 永远不会工作)
我需要找到一个条件,其中每个 API 都被检查并且 .then() 仅针对已解决的 API 运行(在本例中为 Twitter)并忽略失败的 API(在本例中为 Facebook)
if (selectedIds.allowed.TW) {
usersApi.push(TS.loginResource.getTwitterProfiles({
subUserId: selectedIds.allowed.TW,
name: searchTerm
}).$promise);
}
if (selectedIds.allowed.FB || selectedIds.allowed.FB_PAGE ||
selectedIds.allowed.FB_GROUP) {
$scope.post.showTags = true;
usersApi.push(TS.loginResource.getFbPages({
subUserId: selectedIds.allowed.FB_PAGE || selectedIds.allowed.FB
|| selectedIds.allowed.FB_GROUP,
name: searchTerm
}).$promise);
}
if (usersApi.length) {
$q.all(usersApi).then(function (responses) {
var tags1 = responses[0];
tags1.forEach(function (tag, i) {
tags1[i].name = tag.name.replace(/\"/g, "");
});
$scope.post.tags = tags1;
if (usersApi.length > 1) {
var tags2 = responses[1]
tags2.forEach(function (tag, i) {
tags2[i].name = tag.name.replace(/\"/g, "");
});
$scope.post.tags = $scope.post.tags.concat(tags2);
}
})
}
}, 500);
} else {
$scope.post.tags = [];
$scope.post.showTags = false;
}
我认为您正在寻找链接备份响应,以捕获 api 错误和 return 在每个特定 api 调用上新的成功解决承诺,然后再等待 [=15] =] 个。
apiCalls.push(doTwiterStuff().then(handleTwiterSuccess, handleApiFailure);
apiClass.push(doFBStuff().then(handleFbSuccess, handleApiFailure);
Promise.all(apiCalls).then(arr => {
arr.filter(x => !isNil(x)).forEach(x => doSomethingWithApiResult(x));
});
function handleApiFailure(x) {
...
return Promise.resolve(null);
}
希望对您有所帮助。
$q.all
没有弹性
如果其中一个承诺被拒绝,$q.all
将因第一个错误而被拒绝。
要创建一个 弹性 复合承诺,即等待所有承诺完成通过或失败的承诺,请对每个单独的承诺使用 .catch
将被拒绝的承诺转换为成功的承诺。
var resilientPromises = [];
angular.forEach(promises, function(p) {
var resilientP = p.catch( function(result) {
//return to convert rejection to success
return result;
});
resilientPromises.push(resilientP);
});
$q.all(resilientPromises).then( function (results) {
//process results
});
从这个答案中得出的两点:
$q.all
承诺没有弹性。它被第一个拒绝的承诺拒绝。
- 通过返回一个值给
.then
方法或.catch
方法的onRejected函数,可以从被拒绝的promise创建一个已实现的promise .
在我的网站上,我有 API 用于 twitter 和 facebook,它启用 "mentions" 功能(每当我们使用 @ 符号时弹出的功能)
但是,某些功能的访问令牌通常会过期,导致 API 无法正常工作。我将所有 API 存储在一个数组中,然后我需要检查令牌是否失败或未导致解决或拒绝 API 承诺。
这是一个旧代码,由于 $q.all 需要更改。由于 $q.all 在所有承诺都已解决时起作用,从而触发 .then() 调用,这导致 .then() 函数在我的情况下永远不起作用(因为 Facebook API 永远不会工作)
我需要找到一个条件,其中每个 API 都被检查并且 .then() 仅针对已解决的 API 运行(在本例中为 Twitter)并忽略失败的 API(在本例中为 Facebook)
if (selectedIds.allowed.TW) {
usersApi.push(TS.loginResource.getTwitterProfiles({
subUserId: selectedIds.allowed.TW,
name: searchTerm
}).$promise);
}
if (selectedIds.allowed.FB || selectedIds.allowed.FB_PAGE ||
selectedIds.allowed.FB_GROUP) {
$scope.post.showTags = true;
usersApi.push(TS.loginResource.getFbPages({
subUserId: selectedIds.allowed.FB_PAGE || selectedIds.allowed.FB
|| selectedIds.allowed.FB_GROUP,
name: searchTerm
}).$promise);
}
if (usersApi.length) {
$q.all(usersApi).then(function (responses) {
var tags1 = responses[0];
tags1.forEach(function (tag, i) {
tags1[i].name = tag.name.replace(/\"/g, "");
});
$scope.post.tags = tags1;
if (usersApi.length > 1) {
var tags2 = responses[1]
tags2.forEach(function (tag, i) {
tags2[i].name = tag.name.replace(/\"/g, "");
});
$scope.post.tags = $scope.post.tags.concat(tags2);
}
})
}
}, 500);
} else {
$scope.post.tags = [];
$scope.post.showTags = false;
}
我认为您正在寻找链接备份响应,以捕获 api 错误和 return 在每个特定 api 调用上新的成功解决承诺,然后再等待 [=15] =] 个。
apiCalls.push(doTwiterStuff().then(handleTwiterSuccess, handleApiFailure);
apiClass.push(doFBStuff().then(handleFbSuccess, handleApiFailure);
Promise.all(apiCalls).then(arr => {
arr.filter(x => !isNil(x)).forEach(x => doSomethingWithApiResult(x));
});
function handleApiFailure(x) {
...
return Promise.resolve(null);
}
希望对您有所帮助。
$q.all
没有弹性
如果其中一个承诺被拒绝,$q.all
将因第一个错误而被拒绝。
要创建一个 弹性 复合承诺,即等待所有承诺完成通过或失败的承诺,请对每个单独的承诺使用 .catch
将被拒绝的承诺转换为成功的承诺。
var resilientPromises = [];
angular.forEach(promises, function(p) {
var resilientP = p.catch( function(result) {
//return to convert rejection to success
return result;
});
resilientPromises.push(resilientP);
});
$q.all(resilientPromises).then( function (results) {
//process results
});
从这个答案中得出的两点:
$q.all
承诺没有弹性。它被第一个拒绝的承诺拒绝。- 通过返回一个值给
.then
方法或.catch
方法的onRejected函数,可以从被拒绝的promise创建一个已实现的promise .