使用async / await减少太早返回

时间:2017-10-05 20:58:22

标签: javascript async-await reduce

我有这种方法loadAllCustomObjects多次调用async函数loadCustomObject(执行fetch)并减少结果:

export default class CustomObjectLoader {

    static async loadAllCustomObjects(elements) {
        const customObjectTypes = getCustomObjectTypes(elements);
        console.log('loadAllCustomObjects', customObjectTypes);
        const customObjectsData = await customObjectTypes.reduce(async (allData, elementType) => {
            allData[elementType] = await loadCustomObject(elementType)
            return allData;
        }, {});
        console.log('customObjectsData', _.keys(customObjectsData));
        return customObjectsData;
    }

}

但是,reduce过早返回,这是日志语句:

loadAllCustomObjects [ 'slider', 'twitter-share-button' ]
customObjectsData [ 'slider' ]

如何让它完成customObjectTypes中的所有对象?

0 个答案:

没有答案