我有这种方法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
中的所有对象?