rxjs 更新后可观察到的订阅错误

Observable subscription error after rxjs update

这段代码运行良好

export function appInitializer(accountService: AccountService) {
    return () => new Promise(resolve => {
        // attempt to refresh token on app start up to auto authenticate
        accountService.refreshToken()
            .subscribe()
            .add(resolve);
    });
}

但是现在将 rxjs 从版本 6.5.5 更新到 7.4.0 后,当我将 resolve 添加到 Subscription[=29= 时出现此错误] :

(parameter) resolve: (value: unknown) => void Argument of type '(value: unknown) => void' is not assignable to parameter of type 'TeardownLogic'. Type '(value: unknown) => void' is not assignable to type '() => void'.ts(2345) app.initializer.ts(8, 18): Did you mean to call this expression?

我尝试修改的angular项目可用here

我的菜鸟问题是:如何让这段代码再次运行而不必返回到 rxjs v6.5.5?

试试这个

accountService.refreshToken()
        .subscribe()
        .add(()=>resolve());

甚至更好

export function appInitializer(accountService: AccountService) {
    return () => new Promise((resolve,reject) => {
        // attempt to refresh token on app start up to auto authenticate
        accountService.refreshToken().subscribe(()=>resolve,err=>reject(err))
    });

}

但我可能会这样做

export function appInitializer(accountService: AccountService) {
    return () => accountService.refreshToken()
    }

这里有一个工作演示

https://stackblitz.com/edit/angular-gl1dnd?file=src%2Fapp%2Fapp.module.ts

检查控制台,它显示两个初始化器在启动时都是 运行

我认为最好的方法是使用 firstValueFrom:

import { Observable, firstValueFrom } from 'rxjs';

interface AccountService {
  refreshToken: () => Observable<unknown>
}

export function appInitializer(accountService: AccountService) {
    return () => firstValueFrom(accountService.refreshToken());
}