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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ export function asyncResultsAnd(asyncResults: Array<Promise<ValidationResult>>):
return null
}
return new Promise(resolve => {
// 任一不通过,则不通过
let validResultCount = 0
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

return
}
}))
// 所有都通过,则通过
return Promise.all(asyncResults).then(results => {
if (results.every(isValid)) {

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

Expand Down