为了账号安全,请及时绑定邮箱和手机立即绑定

如何从Promise内部返回数据、reduce、format?

如何从Promise内部返回数据、reduce、format?

至尊宝的传说 2023-08-18 10:14:21
一段时间以来,我一直在努力让这个测试通过。我希望它返回一个包含 3 个 mockExpectedResult 对象的数组步骤1:减少计划操作数组(省略没有路径的项目)。这应该返回 InventoryItemPath 的字符串数组第 2 步:减少 InventoryItemPath 数组 (freeRewardsInventory),对服务进行异步调用(getItem 是此异步 GET 请求的模拟),该服务返回 Promise。第 3 步:通过 freeRewardsRaw Promises 进行缩减,格式化为 mockExpectedResult步骤 4:返回输出(mockExpectedResults 数组)我认为我的主要问题是我没有等待所有这些承诺(也许错过了一个等待?)谢谢你的帮助。const mockScheduledOperation = {    Ranks: [{            FreeRewards: {                InventoryRewards: [{                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',                }, ],            },        },        {            FreeRewards: {                InventoryRewards: [{                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',                }, ],            },        },        {            FreeRewards: {                InventoryRewards: [{                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',                }, ],            },        }    ]};const getAllRewards = async () => {    const freeRewardsInventory = mockScheduledOperation.Ranks.reduce(        (agg, rank) => {            if (rank.FreeRewards.InventoryRewards.length > 0) {                const rewardList = rank.FreeRewards.InventoryRewards.reduce(                    (agg, reward) => {                        if (reward.InventoryItemPath) {                            agg = reward.InventoryItemPath;                        }                        return agg;                    },                    ''                );                agg.push(rewardList);            }            return agg;        },        []    );    const getItem = async (rewardPath: string) => mockReturnedItem;    const freeRewardsRaw = freeRewardsInventory.reduce < [] > (        async (agg, rewardPath) => {                const promise = await getItem(rewardPath);                agg.push(promise);                return agg;            },            []    );
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

我试图简化您的代码和reduce. 我已将 s 替换reduce为filters 和maps。请检查一下并告诉我这是否有帮助。


const mockScheduledOperation = {

    Ranks: [{

            FreeRewards: {

                InventoryRewards: [{

                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',

                }, ],

            },

        },

        {

            FreeRewards: {

                InventoryRewards: [{

                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',

                }, ],

            },

        },

        {

            FreeRewards: {

                InventoryRewards: [{

                    InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',

                }, ],

            },

        }

    ]

};

    

const getAllRewards = async () => {

    

    const freeRewardsInventory = 

        ([] as string[])

        // converting multi-dimensional array into uni-dimensional

        .concat(

            ...mockScheduledOperation.Ranks

            .filter(rank => rank.FreeRewards.InventoryRewards.length)

            .map(rank => (

                rank.FreeRewards.InventoryRewards

                    // removing all falsy values

                    .filter(Boolean)

                    .map(item => item.InventoryItemPath)

            )

        )

    );


    const getItem = (rewardPath: string) => mockReturnedItem;



    const freeRewardsRaw = await Promise.all(freeRewardsInventory.map(rewardPath => getItem(rewardPath)))


    const formattedRewards = freeRewardsRaw

        .map < ProgressionRewards[] > ((res) => {

                const formattedReward: ProgressionRewards = {

                    // free = unlocked, paid = locked

                    locked: false,

                    level: null,

                    difficulty: res.CommonData.Quality || null,

                    date: res.CommonData.DateReleased.ISO8601Date || null,

                    rewardAttachments: [{

                        image: res.CommonData.DisplayPath.Media.MediaUrl.Path || null,

                        title: res.CommonData.Title.value || null,

                        description: res.CommonData.Description.value || null,

                        type: res.CommonData.Type || null,

                        released: null,

                        manufacturer: null,

                        howUnlock: null,

                    }, ],

                };

                return formattedReward;

            }, []);

        }

    );


    return formattedRewards;

};



查看完整回答
反对 回复 2023-08-18
  • 1 回答
  • 0 关注
  • 66 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信