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

How to use promise/async in function returning Observable?

我正在使用 nestjs 并想编写一个函数 returning Observable (rxjs) with cache.

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;
      })
    );
  }
}

逻辑很简单,如果有缓存,return缓存,否则调用api,保存到缓存,return结果。唯一的问题是缓存会 return 一个承诺。如何实现?

您可以使用 RxJS from function to convert the Promise to an Observable. From there, you could use switchMap operator + of 函数来 return 用户从缓存中获取数据或进行 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$
    )
  );
}