Async map 返回的是结果和承诺的混合,而不仅仅是结果。

huangapple go评论117阅读模式
英文:

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(&#39;kycPendingProviders:&#39;, kycPendingProviders)
      const incompleteKYCClaims = kycPendingProviders
        .map(async (p) =&gt; {
          const provider = await getProviderOrThrow(p.tin)
          console.log(&#39;provider:&#39;, 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: &#39;KYCInDraft&#39; })
        })
        .filter((claim) =&gt; claim) as unknown as Array&lt;ProviderRelationshipClaims&gt;
      console.log(&#39;incompleteKYCClaims:&#39;, 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 { &lt;pending&gt; } ]` 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(&#39;kycPendingProviders:&#39;, kycPendingProviders);
const incompleteKYCClaims = await Promise.all(
kycPendingProviders.map(async (p) =&gt; {
const provider = await getProviderOrThrow(p.tin);
console.log(&#39;provider:&#39;, 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: &#39;KYCInDraft&#39; });
})
);
const filteredKYCClaims = incompleteKYCClaims.filter((claim) =&gt; claim !== undefined) as Array&lt;ProviderRelationshipClaims&gt;;
console.log(&#39;filteredKYCClaims:&#39;, filteredKYCClaims);

huangapple
  • 本文由 发表于 2023年7月17日 22:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定