我的代码中有错误吗?还是想法不对?
let pollVotesCount, commentVotesCount, sharesCount, commentsCount, blogCount;
const pollVotesPromise = getPollVoteCount(dbClient, userId);
const commentVotesPromise = getCommentVotesCount(dbClient, userId);
const sharesPromise = getShareLinkCount(dbClient, userId);
const commentsPromise = getCommentedCount(dbClient, userId);
const blogPromise = getBlogCount(dbClient, userId);
Promise.all([pollVotesPromise, commentVotesPromise, sharesPromise, commentsPromise, blogPromise]).then((values) => {
pollVotesCount = values[0];
commentVotesCount = values[1];
sharesCount = values[2];
commentsCount = values[3];
blogCount = values[4];
});
// Idea complains that these variables can be uninitialized
if (pollVotesCount >= 1 && commentVotesCount >= 1 && sharesCount >= 1 && commentsCount >= 1) {
finalRank = 'student';
}我的理解是,要么调用then(),要么异常中断执行。
Update:添加循环以提供上下文
for (let i = 0; i < users.length; i += 1) {
user = users[i];
const currentRank = (user.honors) ? user.honors.rank : '';
const userId = user._id;
let finalRank = 'novice';
let pollVotesCount, commentVotesCount, sharesCount, commentsCount, blogCount;
const pollVotesPromise = getPollVoteCount(dbClient, userId);
const commentVotesPromise = getCommentVotesCount(dbClient, userId);
const sharesPromise = getShareLinkCount(dbClient, userId);
const commentsPromise = getCommentedCount(dbClient, userId);
const blogPromise = getBlogCount(dbClient, userId);
Promise.all([pollVotesPromise, commentVotesPromise, sharesPromise, commentsPromise, blogPromise]).then(async (values) => {
pollVotesCount = values[0];
commentVotesCount = values[1];
sharesCount = values[2];
commentsCount = values[3];
blogCount = values[4];
if (!currentRank || currentRank === 'novice') {
if (pollVotesCount >= 1 && commentVotesCount >= 1 && sharesCount >= 1 && commentsCount >= 1) {
finalRank = 'student';
}
} else if (currentRank === 'student') {
// eslint-disable-next-line no-await-in-loop
const positiveCommentsVotesCount = await getPositiveCommentsVotesCount(dbClient, userId);
if (pollVotesCount >= 3 && sharesCount >= 10 && positiveCommentsVotesCount >= 5 && blogCount >= 1) {
finalRank = 'graduate';
}
} else if (currentRank === 'graduate') {
const positivePercent = await getPositivePercent(dbClient, userId);
const consecutiveSharing = await getConsecutiveSharing(dbClient, userId, 10);
if (pollVotesCount >= 10 && consecutiveSharing && positivePercent >= 80 && commentsCount >= 100 && blogCount >= 10) {
finalRank = 'master';
}
} else {
return;
}
const setters = {
'honors.count.poll_votes': pollVotesCount,
'honors.count.comment_votes': commentVotesCount,
'honors.count.comment': commentsCount,
'honors.count.blog': blogCount,
'honors.count.shares': sharesCount,
};
if (currentRank !== finalRank) {
setters['honors.rank'] = finalRank;
}
// eslint-disable-next-line no-await-in-loop
await dbClient.db().collection('users').updateOne({ _id: userId }, { $set: setters });
});
}发布于 2020-08-25 18:47:21
首先,您可以在if then 中调用,而不是分配,您可以通过这种方式对值进行重构。
const pollVotesPromise = getPollVoteCount(dbClient, userId);
const commentVotesPromise = getCommentVotesCount(dbClient, userId);
const sharesPromise = getShareLinkCount(dbClient, userId);
const commentsPromise = getCommentedCount(dbClient, userId);
const blogPromise = getBlogCount(dbClient, userId);
Promise.all([pollVotesPromise, commentVotesPromise, sharesPromise, commentsPromise, blogPromise]).then([
pollVotesCount,
commentVotesCount,
sharesCount,
commentsCount,
blogCount
] => {
if (pollVotesCount >= 1 && commentVotesCount >= 1 && sharesCount >= 1 && commentsCount >= 1) {
finalRank = 'student';
}
});关于最新问题:
将您的函数包装为for loop async async,如下面的所示
async someFunc() { ...for loop... } 然后
const [pollVotesCount,commentVotesCount,sharesCount,commentsCount,blogCount] =
await Promise.all([pollVotesPromise, commentVotesPromise, sharesPromise, commentsPromise, blogPromise])
if (!currentRank || currentRank === 'novice') { // continue your code from here ("then" not needed)https://stackoverflow.com/questions/63585169
复制相似问题