写这个组合值的更好方法

时间:2017-01-19 15:23:31

标签: javascript

我有这段代码。:

const OnLoadStatisticsTypes = loadOnMount('statisticsTypes', fetchStatisticsTypes, 'root')(StatisticBalloons)

export default loadOnMount('appsStatistics', fetchAppsStatistics, 'root')(OnLoadStatisticsTypes)

但我不知道如何做得更好。我试过了。:

export default compose(
  loadOnMount('appsStatistics', fetchAppsStatistics, 'root'),
  loadOnMount('statisticsTypes', fetchStatisticsTypes, 'root'),
  StatisticBalloons
)

但也不行。

如何改进此脚本?

2 个答案:

答案 0 :(得分:1)

这是Redux的compose函数吗?

export default compose(
  loadOnMount('appsStatistics', fetchAppsStatistics, 'root'),
  loadOnMount('statisticsTypes', fetchStatisticsTypes, 'root')
)(StatisticBalloons)

答案 1 :(得分:0)

如果您编写此方法返回承诺,则可以使用Promise.all撰写所有内容。参见示例:

const loadOnMount = (param1, param2, param3) => {
    return new Promise((resolve, reject) => {
    //Your logical here
    })
}

然后你可以这样写:

Promise.all([
  loadOnMount('appsStatistics', fetchAppsStatistics, 'root'),
  loadOnMount('appsStatistics2', fetchAppsStatistics, 'root'),
  loadOnMount('appsStatistics3', fetchAppsStatistics, 'root')
])
.then(function(result){
  //Handle the array with all resolved promises
})