Angular Promise尚未解决

时间:2020-09-07 14:15:47

标签: javascript angular angular-promise

我的班级中有一个subscribe函数,该函数订阅复选框切换的状态:

public subscribe(): void{
   ...
// Subscribe to toggle subject state
    this.subscription = this.isSet$
      .pipe(
        // Skip initial toggle state
        skip(1),
        // Update persisted FCM enabled state
        tap(isSet => {
          localStorage.setItem(PushService.localStorageKey, String(isSet));
        }),
        // Switch between function calls based on the toggle state
        switchMap(isSet => (isSet ?
          this.requestToken()
            .then(() => {
              console.log('RESOLVED REQUEST');
              this.cbDisabled = false;
            }) :
          this.deleteToken()
            .then(() => {
              console.log('RESOLVED DELETE');
              this.cbDisabled = false;
            }))))
      .subscribe();
}

switchMap中的两个承诺如下:

请求

private requestToken(): Promise<any> {

    console.log('REQUEST TOKEN FROM FIREBASE');

    return this.afMessaging.requestToken.pipe(
      mergeMap(deviceToken =>
        this.http.post<void>(
          formatEndpointUrl(environment.endpoints.communicationservice.web_push.register_device),
          {deviceToken}
        )
      )
    ).toPromise();
  }

删除

private deleteToken(): Promise<void> {

    console.log('DELETE TOKEN CALLED');

    return this.afMessaging.getToken.pipe(
      tap((deviceToken) => {
        this.afMessaging.deleteToken(deviceToken);
        console.log('DELETING TOKEN IN FIREBASE');
      }),
      mergeMap(deviceToken =>
        this.http.delete<void>(
          formatEndpointUrl(environment.endpoints.communicationservice.web_push.delete_device, {deviceToken})
        ).toPromise().then(() => {
           console.log('DELETING TOKEN IN BACKEND');
        })
      )).toPromise();
  }

当我将“开关”切换到“关闭”时,它将删除令牌,日志如下所示:

DELETE TOKEN CALLED
DELETING TOKEN IN FIREBASE
DELETING TOKEN IN BACKEND
RESOLVED DELETE

但是,当我将“开关”切换到“开”时,我得到了:

REQUEST TOKEN FROM FIREBASE

就是这样,Promise永远不会得到解决。我想念什么?

0 个答案:

没有答案