我有一個獲取請求,這是一個asynchornouns方法。到取的數據,我做了3秒的延遲,之後,我希望返回結果。下面是代碼:Typescript:延遲後的返回值
getWeatherForcastWithLocation(){
this._http.get(uri)
.subscribe(res =>this.setItems(res.json(),false));
setTimeout(()=>{
return this.weatherStatatistic;
},3000);
return null;
}
我想回this.weatherStatatistic
,但是,它總是返回null
。我知道這是因爲方法結束時return null;
,但是如果我刪除它,方法的返回類型將爲void
。 所以我該如何返回this.weatherStatistic
?
'this.setItems'會發生什麼? – cYrixmorten
它只是在weatherStatistic中設置json值 – Salman
順便說一句,認爲你應該閱讀承諾的概念。通常你會返回這個._ $ http請求,並得到類似'this這樣的響應。 getWeatherForcastWithLocation()。then((res)=> {})'在大括號內處理響應。 – cYrixmorten