【发布时间】:2020-11-18 03:34:53
【问题描述】:
constructor() {
super();
this.state = {
detailWallet: [],
user: [],
historia: [],
namaPertama: [],
namaKirim: [],
}
componentDidMount() {
this.getDetailAPI();
this.getDetailPlaceAPI();
this.getHistoria();
}
getNameAPI = () => {
const id = this.props.match.params.id;
return new Promise(resolve => {
axios.get(`/wallet/user/${id}`)
.then((result) => {
resolve(
this.setState({
namaPertama: result.data.data.content[0].user.firstName
})
)
})
});
}
getHistoria = () => {
console.log(this.getNameAPI())
Promise.all([this.getNameAPI()])
.then(([result]) => {
this.setState({
namaKirim: result
})
})
axios.get(`/wallet/type/6?q=`)
.then((result) => {
this.setState({
historia: result.data.data.content
})
})
}
所以我创建了一个函数,该函数有一个方法 get 包含在 promise 中,当我在 getHistoria 中 console.log 时,promise 给了我一个空数组..
谁能告诉我我的代码有什么问题?我只是了解承诺,所以我仍然不知道,谢谢..
【问题讨论】:
-
Promise.all([this.getNameAPI()]).then(...)- 为什么不必要的Promise.all()电话?就做this.getNameAPI().then(...) -
然后重新引入
Promise.all()来聚合this.getNameAPI()和axios.get()返回的两个promise。并返回聚合的承诺。 -
@Andreas 我试了一下,但没有任何改变,结果仍然给我一个空数组
标签: javascript reactjs api rest promise