如何在函数返回Observable中使用promise/async ?



我正在使用nestjs,并想写一个函数返回Observable(rxjs)与缓存。

import { HttpService } from '@nestjs/axios';
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';
import { map, of, Observable } from 'rxjs';
interface User {
id: string;
// ...
}
@Injectable()
export class Service {
constructor(
@Inject(CACHE_MANAGER) protected cache: Cache,
protected readonly httpService: HttpService,
) {}
fetchUser = (id: string): Observable<User> {
const url = 'xxx';
const user: string = this.cache.get(`user:${id}`); // but here is `Promise<string>` actually
if (user) {
return of(JSON.parse(user) as User);
}
return this.httpService.get<User>(url).pipe(
map(({ data }) => {
this.cache.set(`user:${id}`, JSON.stringify(data));
return data;
})
);
}
}

逻辑很简单,如果有缓存,返回缓存,否则调用api,保存到缓存,返回结果。唯一的问题是缓存会返回一个promise。如何做到这一点呢?

你可以使用RxJS的from函数将Promise转换为Observable。从那里,您可以使用switchMap操作符+of函数返回从缓存中获取的用户或进行HTTP调用。

fetchUser(id: string): Observable<User> {
const url = 'xxx';
const user$ = this.httpService.get<User>(url).pipe(
map(({ data }) => {
this.cache.set(`user:${id}`, JSON.stringify(data));
return data;
})
);
return from(this.cache.get(`user:${id}`)).pipe(
switchMap((user: string) => 
!!user ? of(user) : user$
)
);
}