2017-11-11 37 views
0

我有一個異步調用,它完全調用一個getStatus()函數,對接收到的數據執行一些計算,然後返回計算的數據。我正在調用load()函數,並且我想要計算它的數據。JavaScript - 如何從訂閱中返回數據

load(case, start, end) { 
    Observable.forkJoin (
    this.http.get('/workschedules'), 
    this.http.get('/block?startDate=' + start + '&endDate=' + end), 
    this.http.get('/filed?caseId=' + case+ '&startDate=' + start + '&endDate=' + end) 
).subscribe(data => { 
    this.works$ = data[0]; 
    this.block$ = data[1]; 
    this.avail$ = data[2]; 
    }, err => console.log(err),() => this.getStatus(case, 217380, start, end, 5)); 
} 

getStatus(caseId, appId, start, end, capacity: number) { 
    //some calculation on all the subscribed data 
    return calculated_data; 
} 

我想load()函數調用的數據 例如。

let temp = load(12512,'22/10/2017','25/10/2017'); 

回答

0

你不能混合異步與同步的東西。

鑑於http是異步的,您的函數不得不返回異步數據(將在某些時候解析)。這將是這樣的:

load(...): Observable<any> { 
    return Observable.forkJoin(...) // without subscribe 
} 

load(...).subscribe((data) => ...); 
+0

如何獲得異步調用計算的數據。 –

+0

@ FarhanAnsari你通過'subscribe'收集它 – zurfyx

相關問題