2

I am trying to return an observable inside an async arrow function passed to a flatMap, but the returned observable is not being called.

protected buildUseCaseObservable(params: LoginUserParams): Observable<Session> {
        return this.userRepository.getUserByName(params.getUsername())
            .pipe(flatMap(async user => {
                if (!user) {
                    throw new Error(Errors.USER_DOESNT_EXIST);
                }

                const match = await this.cypher.compare(params.getPassword(), user.password);
                if (!match) {
                    throw new Error(Errors.WRONG_PASSWORD);
                }

                return Observable.create((subscriber: Subscriber<Session>) => {
                    subscriber.next(new Session("token test", "refreshToken test"));
                    subscriber.complete();
                });
            }));
    }

Does anyone knows why does it happen and how can I solve it? Thanks in advance.

dieppa
  • 166
  • 1
  • 9

1 Answers1

1

Solved, I just turned the promise into an observable and did flatMap it.

protected buildUseCaseObservable(params: LoginUserParams): Observable<Session> {
        return this.userRepository.getUserByName(params.getUsername())
            .pipe(flatMap(storedUser => {
                if (!storedUser) {
                    throw new Error(Errors.USER_DOESNT_EXIST);
                }

                return from(this.cypher.compare(params.getPassword(), storedUser.password));
            })).pipe(flatMap(match => {
                if (!match) {
                    throw new Error(Errors.WRONG_PASSWORD);
                }

                return Observable.create((subscriber: Subscriber<Session>) => {
                    subscriber.next(new Session("token test", "refreshToken test"));
                    subscriber.complete();
                });
            }));
    }
dieppa
  • 166
  • 1
  • 9