Promise Reject Callback is not called but Resolve Callback is being called as expected

Viewed 35

So I have a problem regarding Promises currently and I'm not sure why it is not working as expected.

In my Angular Library I have the following code:

public async load() {
    return new Promise<void>((resolve, reject) => {
      this.service.isReady$
        .pipe(
          take(2),
          filter((result) => result.ready),
          switchMap(() => {
            return this.loadClassifierFiles();
          }),
          tap(() => {
            console.log('Classifier Files should load now');
          })
        )
        .subscribe({
          next: () => {
            try {
              // do stuff here ...
              resolve();
            } catch {
              reject(new Error('Could not load all Classifier Files'));
            }
          },
          error: () => {
            reject(
              new Error('Could not fetch all Classifier Files from Destination')
            );
          },
        });
    });
  }

In my Application I have the following code:

public async changeSomething() { 
    await this.service
      .changeStuff()
      .then(() => {
        this.modelStatus = ModelChangeStatus.SUCCESS;
      })
      .catch((error) => {
        this.modelStatus = ModelChangeStatus.FAILURE;
      });
  }

In the test of my Library I have verified that for example on an error when loading the files during this.loadClassifierFiles() I will get into the error block of my subscription and therefore the Promise should be rejected. However, only when I resolve the Promise the callback in my Application is called. When I reject the Promise only the error handling of the library is being called but not the catch block in my application that I need for a status update.

I also tried the following but nothing changed:

 this.openCvService
      .changeStuff(tempConfig)
      .then(
        () => {
          this.modelStatus = ModelChangeStatus.SUCCESS;
        },
        (error: Error) => {
          console.log(error.message);
          this.modelStatus = ModelChangeStatus.FAILURE;
        }
      )

Any ideas why the rejection callback is not being called?

0 Answers
Related