값을 반환하는 메서드가 있고 반환 값을 변수에 할당하고 있습니다. 함수가 적절한 값을 반환하지만 변수가 반환 값으로 설정되지 않았습니다. 그러나 this.size
함수 내부에 설정 하면 적절한 값을 얻습니다. 나는 this.searchData
많은 함수에서 함수를 호출 하고 있으므로 이것을 할 수 없습니다 (이 경우 다중 if
문 을 사용해야 합니다). 따라서 함수에서 반환 값을 얻는 방법이 있습니까? 내가 사용에 생각 timeout
다른 방법으로 주위가?
searchSize(event): void {
this.sizes = this.searchData(event, AppConsts.size);
}
searchData(event, property): any {
this._serviceProxy.getProperties(
property,
event.query,
event.query
).subscribe((result) => {
console.log(result.items);
return result.items;
});
}
더 깔끔한 코드를 위해 async / await 구문을 사용할 수도 있습니다. Observable을 promise로 변환해야하며 결과를 기다릴 수 있습니다.
import * as rx from 'rxjs'
class ListResultDtoOfPropertyDTO{
items: any[]
}
class Test {
sizes: any[];
_serviceProxy : {
getProperties() : rx.Observable<ListResultDtoOfPropertyDTO>
}
async searchSize(event): Promise<void> {
this.sizes = await this.searchData(event);
}
async searchData(event): Promise<any[]> {
var result = await this._serviceProxy.getProperties()
.toPromise()
console.log(result.items);
return result.items;
}
}
설명 :
Typescript는 Promise의 결과를 기다리는 데 더 좋은 구문을 허용합니다. 구문은 Observable에 적용되지 않습니다 (향후에는 그렇게 될 것입니다). 그러나이 경우 Observable을 promise로 변환 할 수 있습니다. async / await 구문에 대한 자세한 내용은 여기를 참조하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다