3 回答

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});
}

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結構。否則其他答案更好。
添加回答
舉報