0
我需要进行多个依赖于对方的异步调用。我最初编写代码并使用Promise.all
分步制作async
。我绕过了我的数据并创建了一个async
方法,以便将所有需要的操作放入一个数组中传递到Promise.all()
。这工作正常,但我怎么能使用Observables来做同样的事情。我读过forkJoin
相当于Promise.all
,但是如何循环访问数据并包装我的async
函数,然后在执行之前执行它,然后转到下一个flatMap
?将Promise.all转换为Observable
public getMonthly(){
return this.http.get(url)
.flatMap(response => {
// need to convert this?
let actions = response.json().map(this.asyncMonthlyRecord);
return Promise.all(actions);
})
.flatMap(()=> this.queryMonthly())
.map(this.convertFromSQl)
.catch((error:any)=> Observable.throw(error || 'Server Error'));
}
private asyncMonthlyRecord = (record):Promise<any> => {
return this.setUsage(record,'HILowMonthly');
}
private queryMonthly(){
return this.storage.query('SELECT * FROM HILowMonthly')
}
getMonthly().subscribe(x => console.info(x)); // logs data from SQLite perfectly...
为什么要投票? – inspired