亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從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 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號