繼this之後,我想要做一些很長的過程,並且then
在完成過程後設置我的狀態。如何聲明一個函數變量並等待它完成?
我做下面的程序:
constructor(props) {
super(props);
let MyParameter = this.props.navigation.state.params.Whatever;
getResults(MyParameter).then((response) => {this.setState({isLoading: false, result: response })});
this.state = {
isLoading: true,
result: null,
resultDS: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }),
}
}
// ..
async getResults(parameter: Object)
{
let finalResult = [];
await var myfunc = function() { // Do the long process and populate finalResult}
return finalResult;
}
我跟着var functionName = function() {} vs function functionName() {}和When should I store a function into a variable?和我仍然得到錯誤:
UnExpected token on line
await var myfunc = function() { /* ... */ }
我怎樣才能解決這個問題呢?
'var myfunc = function(){...}'聲明一個函數。當你想等待一個異步函數完成時,你只使用'await'即你調用一個函數。 –
@MikeC所以像'var myFunc = function(){}'然後'返回await myFunct()'? –