When calling two functions, I make use of fn1
and fn2
. To execute them one after the other, I utilize concatMap
.
I choose not to use exhaustMap
and switchMap
as they can result in nested "callback-hell".
exhaustMap(() =>
fn1().pipe(
switchMap(() => fn2()))))
The main issue is how do I retrieve the results of fn1
and fn2
for the next
function that follows the invocation of fn2?
Visit stackblitz.com for more.
import { of } from 'rxjs';
import { concatMap, tap } from 'rxjs/operators';
const fn1 = () => {
console.log('Executing fn1');
return of('fn1');
};
const fn2 = () => {
console.log('Executing fn2');
return of('fn2');
};
of(1)
.pipe(
concatMap(() => fn1()),
concatMap(() => fn2()),
tap({
next: (result) => {
console.log({ result }); /// <---- I want to access fn1 and fn2 here.
console.log('Inside tap function!!!');
},
error: () => {
console.log('Error inside tap function!!!');
},
complete: () => {
console.log('Completed tap function');
},
})
)
.subscribe({
next: (output) => {
console.log({ output });
},
error: (error) => {
console.log({ error });
},
complete: () => {
console.log('Process Complete');
},
});