【发布时间】:2019-03-17 22:52:45
【问题描述】:
我有一个问题:
我希望我的axios 提出申请,然后提出this.setState 并将结果保存在变量中。
我的代码:
componentDidMount() {
let mails = [];
axios.get('/api/employee/fulano')
.then(res => this.setState({
employees: res.data
}, () => {
this.state.employees.map(i => {
async axios.get(`/api/status/${i.mail}`)
.then(res => {
mails.push(res.data)
await this.setState({
mails: mails
})
})
.catch(err => console.log(err))
})
}))
.catch(err => console.log(err))
}
但它给出了错误语法。
最佳解释:我想将地图的所有结果保存在变量mails 中,稍后使用setState 更改只是一次的结果。
谁能告诉我我在哪里徘徊?请。
【问题讨论】:
-
如果您可以使用
then和catch,为什么还要尝试使用async/await? -
为解决您的实际问题,您正在寻找
Promise.all
标签: javascript reactjs async-await axios