簡體   English   中英

如何從 Promise、reduce、format 內部返回數據?

[英]How to return data from inside Promise, reduce, format?

我一直在努力讓這個測試通過一段時間。 我希望它返回一個包含 3 個 mockExpectedResult 對象的數組

步驟 1:減少計划操作數組(省略沒有路徑的項目)。 這應該返回 InventoryItemPath 的字符串數組

第 2 步:減少 InventoryItemPath 數組 (freeRewardsInventory),對返回 Promise 的服務進行異步調用(getItem 是此異步 GET 請求的模擬)。

第 3 步:Reducer over 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;
            },
            []
    );

    const formattedRewards = await Promise.all(freeRewardsRaw).then(
        (response) => {
            response.reduce < ProgressionRewards[] > ((agg, 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,
                    }, ],
                };
                agg.push(formattedReward);
                return agg;
            }, []);
        }
    );

    return formattedRewards;
};

const mockExpectedResult: ProgressionRewards = {
    locked: false,
    level: null,
    difficulty: ChallengeLevel.Easy,
    date: '',
    rewardAttachments: [{
        image: 'media-image-path',
        title: 'MIA',
        description: 'reach-mia',
        type: 'ArmorVisor',
        released: null,
        manufacturer: null,
        howUnlock: null,
    }, ],
};

fit('free rewards to return an array of rewards', async () => {
    const awards: ProgressionRewards = await getAllRewards();

    expect(awards).toBe([
        mockExpectedResult,
        mockExpectedResult,
        mockExpectedResult,
    ]);
});

我試圖簡化您的代碼和reduce的問題。 我已經用filter s 和map s 替換了reduce s。 請檢查一下,讓我知道這是否有幫助。

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;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM