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

一段时间以来,我一直在努力让这个测试通过。我希望它返回一个包含 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;

            },

            []

    );

至尊宝的传说
浏览 68回答 1
1回答

梵蒂冈之花

我试图简化您的代码和reduce. 我已将 s 替换reduce为filters 和maps。请检查一下并告诉我这是否有帮助。const mockScheduledOperation = {&nbsp; &nbsp; Ranks: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeRewards: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryRewards: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeRewards: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryRewards: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeRewards: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryRewards: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]};&nbsp; &nbsp;&nbsp;const getAllRewards = async () => {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const freeRewardsInventory =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ([] as string[])&nbsp; &nbsp; &nbsp; &nbsp; // converting multi-dimensional array into uni-dimensional&nbsp; &nbsp; &nbsp; &nbsp; .concat(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...mockScheduledOperation.Ranks&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(rank => rank.FreeRewards.InventoryRewards.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(rank => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rank.FreeRewards.InventoryRewards&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // removing all falsy values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(Boolean)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(item => item.InventoryItemPath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; &nbsp; const getItem = (rewardPath: string) => mockReturnedItem;&nbsp; &nbsp; const freeRewardsRaw = await Promise.all(freeRewardsInventory.map(rewardPath => getItem(rewardPath)))&nbsp; &nbsp; const formattedRewards = freeRewardsRaw&nbsp; &nbsp; &nbsp; &nbsp; .map < ProgressionRewards[] > ((res) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const formattedReward: ProgressionRewards = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // free = unlocked, paid = locked&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locked: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; level: null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; difficulty: res.CommonData.Quality || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: res.CommonData.DateReleased.ISO8601Date || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rewardAttachments: [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image: res.CommonData.DisplayPath.Media.MediaUrl.Path || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: res.CommonData.Title.value || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: res.CommonData.Description.value || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: res.CommonData.Type || null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; released: null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; manufacturer: null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; howUnlock: null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return formattedReward;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, []);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );&nbsp; &nbsp; return formattedRewards;};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript