首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Idea认为在Promise.all()中初始化的变量没有初始化

Idea认为在Promise.all()中初始化的变量没有初始化
EN

Stack Overflow用户
提问于 2020-08-25 18:39:17
回答 1查看 31关注 0票数 0

我的代码中有错误吗?还是想法不对?

代码语言:javascript
复制
  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:添加循环以提供上下文

代码语言:javascript
复制
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 });
  });
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-25 18:47:21

首先,您可以在if then 中调用,而不是分配,您可以通过这种方式对值进行重构。

代码语言:javascript
复制
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,如下面的所示

代码语言:javascript
复制
async someFunc() { ...for loop... } 

然后

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63585169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档