Reputation: 1542
How can I implement the following code using Observables in rxjs?
What I am trying to achieve here is that I have an array of functions, each of which accepts an object, modifies it and returns the object to the next function in the stack.
function A(res:SomeType){
//Do Something
return res;
}
function B(res:SomeType){
//Do Something
return res;
}
function C(res:SomeType){
//Do Something
return res;
}
let fnPipe = [];
fnPipe.push(A);
fnPipe.push(B);
fnPipe.push(C);
obj= {key:"val"};
fnPipe.forEach((fn)=>{
obj= fn(obj);
});
console.log(obj);
How can I implement the same using observables in rxjs?
Upvotes: 0
Views: 57
Reputation: 682
let fn$ = Observable.from([
x => x + "a",
x => x + "b",
x => x + "c"
])
let value$ = Observable.of("x", "y", "z")
value$
.concatMap(val => fn$.scan((acc, fun) => fun(acc), val))
.subscribe(console.log)
/* prints
"xa"
"xab"
"xabc"
"ya"
"yab"
"yabc"
"za"
"zab"
"zabc" */
Upvotes: 1