다음 이 , 좀 긴 과정을 수행 할 및 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;
}
I는 다음 함수 FUNCTIONNAME () {} VS VAR의 FUNCTIONNAME = 함수 () {} 및 I를 가변으로하는 기능을 저장해야하는 경우? 그리고 여전히 오류가 발생합니다.
예상치 못한 토큰 온라인
await var myfunc = function() { /* ... */ }
이 문제를 어떻게 해결할 수 있습니까?
이 같은?
async getResults(parameter: Object)
{
let finalResult = [];
const myFunc = async function() { /* Do the long process and populate finalResult */ };
await myFunc();
return finalResult;
}
또는 더 깨끗한 방법은 완료 후 장기 실행 프로세스 함수가 finalResult를 반환하도록하는 것이므로 myFunc 외부와 관련이없는 경우 getResults 범위에서 finalResult를 유지할 필요가 없습니다.
async getResults(parameter: Object)
{
const myFunc = async function() { /* Do the long process and return finalResult */ };
return myFunc();
}
await
반환시 키워드는 비동기 함수를 반환하는 비동기 함수와 중복되므로 필요하지 않습니다.
장기 실행 프로세스가 조기에 반환되지 않도록하는 것이 중요하므로 그 안의 어떤 것이 콜백을 사용하거나 비동기 적이라면이를 수용해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다