sane way to pass/keep a value throughout a long pipe
The way suggested by @René Winkler is the right way for short pipes.
If the pipe is long though it can be tedious and somehow can make the code less readable.
One alternative approach can be to create a function (or method) where you define id
as a local variable that you set within the first map
so that you can use it at your convenience all along the chain of operators, something like
getLongPipe(id) {
let _id;
return start$.pipe(
map((id)=> {_id = id; ....}), //I want to save the "id" value to be used in the end of the pipe
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...)//Here you can use _id
)
}
getLongPipe(id).subscribe()
I think the correct way is to make another closure
const processId = (id) => observableOf(id)
.pipe(
map(() => { ... }),
map(...),
switchMap(...),
map(...),
switchMap(...),
map(...),
switchMap(...),
switchMap(...) // Use id here
);
const getPipe = () => start$
.pipe(switchMap(processId));
Storing local variable as a side effect in getPipe
is OK, but it can break if start$
Observable emits more values.