Reputation: 1927
I have 2 functions that return Observables that I would like to execute one after another. function1 return
Observable<SomeDataObject>
and function2 return type
Observable<Any>
I want that the function that execute them return boolean based of the result of function1. I was managed to do so when running first function1, but now I would like to run function2 first, and I get the error:
"Argument of type '(res: <SomeDataObject>) => boolean' is not assignable to
parameter of type '(value:SomeDataObject, index: number) =>
ObservableInput<any>. Type 'boolean' is not assignable to type
ObservableInput<SomeDataObject>
See my code: This works:
return this.someService.function1().pipe(
switchMap(f1Result =>
{
this.someService.repositpry.f1ResultData = f1Result;
return this.someService.function2().pipe(
map(res =>
{
if (res) return true;
else return false;
}));
})
)
This fails:
return this.someService.function2.pipe(
switchMap(f2Result =>
{
this.someService.function1().pipe(
map(f1result =>
{
this.someService.repositpry.f1ResultData = f1Result;
})
);
if (f2Result) return true
else return false;
}));
Upvotes: -2
Views: 682
Reputation: 17762
In your second example, within the most external pipe, you have
this.someService.function1().pipe(
map(f1result =>
{
this.someService.repositpry.f1ResultData = f1Result;
})
f1result
is an Observable. In the inner pipe you use a map
operator. This is different from the first example, the one which is working, since in the first example you use switchMap
, which flattens the Observable.
So you may have to use switchMap
in the inner pipe as well, followed by a map
to return the result you are looking for, something like this
return this.someService.function2.pipe(
switchMap(f2Result =>
{
this.someService.function1().pipe(
switchMap(f1result =>
{
this.someService.repositpry.f1ResultData = f1Result;
}.pipe(() => {
if (f2Result) return true
else return false;
}))
);
}));
Upvotes: 1
Reputation: 44356
You do:
this.someService.function2.pipe(
//...
)
Should it not be:
this.someService.function2().pipe(
//...
);
Upvotes: 1
Reputation: 1218
You need a concatMap operator to do this.
return this.someService.function1().pipe(
concatMap(f1Result =>
{
this.someService.repositpry.f1ResultData = f1Result;
return this.someService.function2.pipe(
map(res =>
{
if (res) return true;
else return false;
}));
})
)
Upvotes: 1