继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()'? –