Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update logic of resolve async results #79

Merged
merged 4 commits into from
Mar 11, 2022
Merged

Conversation

Zenas-He
Copy link
Contributor

No description provided.

src/utils.ts Outdated
@@ -15,18 +15,19 @@ export function asyncResultsAnd(asyncResults: Array<Promise<ValidationResult>>):
return null
}
return new Promise(resolve => {
// 任一不通过,则不通过
let count = 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let count = 0
let validResultCount = 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

asyncResults.forEach(asyncResult => asyncResult.then(result => {
// return error if any result is invalid
if (!isValid(result)) {
resolve(result)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好 return 下,invalid 的话就不应该执行后边的逻辑了(虽然在目前的代码下不会导致问题)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

src/utils.ts Outdated
if (!isValid(result)) {
resolve(result)
validResultCount = -1
Copy link
Collaborator

@nighca nighca Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣 我不是指第 i 个 result invalid 的话,就不去处理后边的 i+1, i+2, ... 的 result,那个处理了也没事的,最多就是多执行两行代码,不会影响逻辑的正确性

我是指,在对第 i 个 result 进行处理的时候,如果它 invalid,就不应该执行后边的:

validResultCount++
// pass if all results are valid
if (validResultCount === asyncResults.length) {
  ...
}

这会引入一些逻辑上的潜在风险,比如 validResultCount 值不能正确地匹配它的定位,对于它的消费有可能会导致 bug

现在设置 validResultCount-1 以跳过 i+1, i+2, ... 的做法会让逻辑本身变复杂,我个人倾向可以不要这个逻辑?

Copy link
Contributor Author

@Zenas-He Zenas-He Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉了,仅保留 return

Copy link
Collaborator

@nighca nighca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥪

@nighca nighca merged commit 370267b into qiniu:master Mar 11, 2022
@Zenas-He Zenas-He deleted the async branch March 12, 2022 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants