![](/img/trans.png)
[英]How to call function only after the executing of the other function is completly finished
[英]How to call another method only after first method is completly done
我遇到一種情況,我需要在一種方法中使用數組,該方法先前已在第一種方法中填充。
我需要兩種方法都可以在componentDidMount
上執行,例如加載國家/地區,然后在droopdown中加載當前國家/地區的城鎮。 這樣的事情。
可以說我需要分別調用這兩個方法:
async componentDidMount() {
this.loadCountries();
this.loadCities(this.state.currentCountry.id);
}
我如何確定第一種方法是執行完畢,因為在第二種方法中我需要使用countries array countries[],
。
現在,我有從服務器檢索情況數據,但是第二種方法不知道它,因為數據可能是異步檢索的,僅此而已。
多謝你們
干杯
只有在第一個函數完成后,才能使用async/await
方法調用第二個函數。
這是你的摘錄。
var countries = [];
async componentDidMount() {
await this.countries = this.loadCountries(); // you will have countries values in 'this.countries'. Which you can access it further .
await this.loadCities(this.state.currentCountry.id);
}
如果您在第一種方法中返回的是coutries []的全局列表,則第二種方法將知道
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.