英文:
Async map returns a mix of results and promises instead of just results
问题
我有以下的地图和筛选功能在异步中运行。
console.log('kycPendingProviders:', kycPendingProviders)
const incompleteKYCClaims = kycPendingProviders
.map(async (p) => {
const provider = await getProviderOrThrow(p.tin)
console.log('provider:', provider)
// 检查提交当前KYC的人
if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return Promise.reject()
return toProviderRelationshipClaims({ tin: p.tin, role: ProviderUserSubRoles.ky, status: 'KYCInDraft' })
})
.filter((claim) => claim) as unknown as Array<ProviderRelationshipClaims>
console.log('incompleteKYCClaims:', incompleteKYCClaims)
尝试了`if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return` 和 `filter` 将会移除 `undefined` 条目的想法以及 `if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return Promise.reject()` 版本。
当条件不满足时,我得到的结果是 `[ Promise { <pending> } ]`,而我需要在后续的 `filter()` 中将 `undefined` 值从结果数组中移除。
我有一个使用 `for of` 循环的版本,那个版本似乎工作正常。为什么 `map()` 在这里不返回一个值 [这里是`undefined`],而是一个未决的 promise?我该如何解决这个问题?
谢谢
<details>
<summary>英文:</summary>
I have the below map and filter functions doing async inside.
console.log('kycPendingProviders:', kycPendingProviders)
const incompleteKYCClaims = kycPendingProviders
.map(async (p) => {
const provider = await getProviderOrThrow(p.tin)
console.log('provider:', provider)
// check who submitted the current KYC
if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return Promise.reject()
return toProviderRelationshipClaims({ tin: p.tin, role: ProviderUserSubRoles.ky, status: 'KYCInDraft' })
})
.filter((claim) => claim) as unknown as Array<ProviderRelationshipClaims>
console.log('incompleteKYCClaims:', incompleteKYCClaims)
Tried both `if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return` with the idea that `filter` will remove `undefined` entries and `if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) return Promise.reject()` versions.
When the condition is not met, I am getting as a result `[ Promise { <pending> } ]` while I need an `undefined` value to be removed from the results array by the subsequent `filter()`.
I have a version with `for of` loop. That seemed to be working correctly. Why is `map()` not returning a value [here `undefined`], but a pending promise? How do I fix this?
Thank you
</details>
# 答案1
**得分**: 1
要解决这个问题并获得所需的行为,您可以使用Promise.all()来等待map()返回的所有promise,然后在已解析的值上使用filter()函数。这里是一个示例:
```javascript
console.log('kycPendingProviders:', kycPendingProviders);
const incompleteKYCClaims = await Promise.all(
kycPendingProviders.map(async (p) => {
const provider = await getProviderOrThrow(p.tin);
console.log('provider:', provider);
if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) {
return undefined; // 或者 null,或者表示删除的任何其他值
}
return toProviderRelationshipClaims({ tin: p.tin, role: ProviderUserSubRoles.ky, status: 'KYCInDraft' });
})
);
const filteredKYCClaims = incompleteKYCClaims.filter((claim) => claim !== undefined) as Array<ProviderRelationshipClaims>;
console.log('filteredKYCClaims:', filteredKYCClaims);
希望这对您有所帮助。
英文:
To fix this issue and obtain the desired behavior, you can use Promise.all() to await all the promises returned by map() and then use the filter() function on the resolved values. Here's an example:
console.log('kycPendingProviders:', kycPendingProviders);
const incompleteKYCClaims = await Promise.all(
kycPendingProviders.map(async (p) => {
const provider = await getProviderOrThrow(p.tin);
console.log('provider:', provider);
if (provider.kycSubmissionInfo?.submissionRequest.submitterUUID !== currentUserID) {
return undefined; // or null, or any other value that signifies removal
}
return toProviderRelationshipClaims({ tin: p.tin, role: ProviderUserSubRoles.ky, status: 'KYCInDraft' });
})
);
const filteredKYCClaims = incompleteKYCClaims.filter((claim) => claim !== undefined) as Array<ProviderRelationshipClaims>;
console.log('filteredKYCClaims:', filteredKYCClaims);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论