3 回答
![?](http://img1.sycdn.imooc.com/5458643d0001a93c02200220-100-100.jpg)
TA贡献1878条经验 获得超4个赞
这个怎么样
async myMethod() {
var self = this;
var myArray = [];
var result = await getCustomers();
for(var index = 0; index < result.length; index++) {
var booking = await this.getBookings(result[index].customerId);
myArray.push(<div className="col">
{result[index].customerId}
{booking}
</div>
}
self.setState({customers: myArray});
}
![?](http://img1.sycdn.imooc.com/545845b40001de9902200220-100-100.jpg)
TA贡献1841条经验 获得超3个赞
正如您所做的那样getCustomers(),您必须获得 with 的承诺结果then。所以你的代码看起来像这样:
myMethod() {
var self = this;
var myArray = [];
getCustomers().then(result => {
for(var index = 0; index < result.length; index++) {
this.getBookings(result[index].customerId).then(bookings => {
myArray.push(<div className="col">
{result[index].customerId}
{bookings}
</div>);
});
}
self.setState({customers: myArray});
});
}
请注意,此解决方案假定您没有使用 ES6async/await结构。否则其他答案更好。
添加回答
举报