【发布时间】:2018-07-16 09:59:41
【问题描述】:
我正在尝试使用 async/await 并且我的代码有效,我只需要有关使用 async/await 的正确方法的建议,还是我需要 .then
这里。我有一个函数loadDataForExport - 它返回带有生成数据的承诺。另外,我有带有异步事件功能的actionButtons,我使用 asyn/await 因为在其他情况下let data = loadDataForExport() - 返回承诺。
loadDataForExport = async () => {
const {sort, search} = this.state
let dataForExport = await this.props.load({pageSize: this.props.totalCount, skip: 0, sort, search}).then(({entities: {devices, rooms}}) => Object.values(devices).map(({deviceType, deviceId, name, version, location, lastAliveMessage, deviceStatus}) => ({
deviceType,
deviceId,
name,
currentVersion: version,
location: location && rooms[location] ? rooms[location].name : '',
lastAliveMessage,
deviceStatus,
})))
return dataForExport
}
const actionButtons = loadDataForExport => [
{
event: async () => {
let data = await loadDataForExport()
exportToExcel(data)
},
name: 'exportToExcel',,
},
]
【问题讨论】:
-
是的,对我来说看起来不错。
标签: javascript asynchronous ecmascript-6 async-await ecmascript-2017