balteo balteo - 1 year ago 132
Javascript Question

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
{unavailable: true}
from the above but not an observable?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download