在我的网站上,我有用于twitter和facebook的API,这些API启用了“提及”功能(每当我们使用@符号时都会弹出的功能)
但是,某些功能的访问令牌经常会过期,从而导致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;
}
答案 0 :(得分:1)
我认为您正在寻找可以捕捉到api错误的备份响应,并在等待所有“ api”调用之前,对每个特定的api调用返回一个成功解决的新承诺。
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);
}
希望这会有所帮助。
答案 1 :(得分:1)
$q.all
没有弹性 1 如果其中一个承诺被拒绝,则$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函数值来从被拒绝的承诺中创建已兑现的承诺。