【发布时间】:2019-06-16 03:51:57
【问题描述】:
在我的网站中,我有用于 twitter 和 facebook 的 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;
}
【问题讨论】: