Reputation: 331
This is my code. i have to validate userno & email while submit form. But userno exist is conditional based on user Type and emailvalidation common for all users..
THis is code
save()
{
numexist:false;emailexist:false;
if(type=="Manual")
{
this.userService.IsUserNoExist(no).subscribe({
next: (result: any) => {
numexist=result;
},
error: (error:any) => {
this.close();
}
});
} //if
//Email Check common for all user type
this.userService.IsEmailExist(email).subscribe({
next: (result: any) => {
emailexist=result;
},
error: (error:any) => {
this.close();
}
});
if(numexist==false && emailexist==false)
{ // Here comes before result come from email exist
//Save
}
else
{
//nothify
}
}
I Have written multiple subsribe method..but i have to check both condition process save ..please let me know which operator i have to join forkjoin not for optional case please help me
Upvotes: 0
Views: 180
Reputation: 60538
Something like this (exact syntax not tested):
userNoExists$ = iif(()=> type==="Manual",
this.userService.IsUserNoExist(no).pipe(
tap((result: any) => numexist=result),
catchError((error:any) => this.close())
),
of(null)
);
If the type is "manual", call the service, process the result, and emit the value. Otherwise, emit null (or this could emit false).
For more information on iif
see: https://rxjs.dev/api/index/function/iif
Then
emailExists$ = this.userService.IsEmailExist(email).pipe(
tap((result: any) => emailexist=result),
catchError((error:any) => this.close())
);
And
save() {
combineLatest([
this.userNoExists$,
this.emailExists$
]).pipe(
map([numexist, emailexist]) => {
if(numexist==false && emailexist==false)
{ // Save
}
else
{
//nothify
}
})
).subscribe();
}
Upvotes: 1