我有一个已获取对象的缓存.我的函数getListByIds
应该返回Observable<Model>
.在这个函数中,我需要判断对象是否存在于缓存中(按id判断),如果不存在,则从后端获取它.使用async/await
的代码可能是:
public async getListByIds(ids: number[]): Promise<Model> {
return ids.map(id => {
let cached = this.cacheService.get(id); // may return undefined
if (!cached) cached = await this.http.get(baseUrl + '/get/' + id).toPromise();
return cached;
});
}
我需要将其转换为RxJS-to Promise()已被弃用,我想学习RxJS.
Edit
让我们假设我想在缓存中存储就绪模型,并且从API中有需要解析的纯JOSN数据.
public async getListByIds(ids: number[]): Promise<Model> {
return ids.map(id => {
let cached = this.cacheService.get(id); // may return undefined
if (!cached) {
const apiData = await this.http.get(baseUrl + '/get/' + id).toPromise();
cached = this.parseApiData(apiData);
}
return cached;
});
}
我应该在哪里打parseApiData()
?