Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS clarification needed: how to return a plain object instead of an Observable

I am writing an angular 2 validator function. I would like to return a plain object instead of an observable.

Here is my current implementation:

import {AbstractControl} from "@angular/common";
import {UserAccountService} from "../../useraccount/useraccount.service";

export function validateEmailAvailable(userAccountService: UserAccountService) {
  return (control: AbstractControl)=> { //This returned lambda should itself return a plain object
    return userAccountService.checkAvailability(control.value)
      .map(res=> {
        if (res.json() === true) {
          return null;
        }
        else {
          return {unavailable: true};
        }
      });
  };
}

Can someone please explain how to properly use the RxJs operators in order to return either null or {unavailable: true} from the above but not an observable?

like image 283
balteo Avatar asked Oct 14 '25 18:10

balteo


1 Answers

I would bet that this is not possible. This is the same as asking to ask an async function to return its value synchronously. Or returning a value in the present computed from values in the future. It is inherently contradictory. Promise 'operators' will always also return a promise, if you think about it.

like image 99
user3743222 Avatar answered Oct 17 '25 08:10

user3743222



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!