I've been struggling a bit with the RxJS Observable.
Here's the scenario:
There's an interface (just for clarity) for my objects:
interface MyObject
{
value: number;
new_value?: number;
}
Next, there's a small function that takes an object and returns an observable to update the object:
let updateObject = (object: MyObject) => {
return Observable.create(observer => {
setTimeout(() => {
object.new_value = object.value * 5;
observer.next(object);
observer.complete();
}, 1500);
});
};
Now I have a source observable that returns an array of objects (e.g., as a response from a server):
let observable = Observable.of<MyObject[]>([
{value: 1},
{value: 2},
{value: 3}
]);
So, my goal is to update each object in the array using my "updateObject()" method. When I subscribe to the observable, I expect this result:
// here the asyncOperation should be applied to the observable
observable.subscribe((objects: MyObject[]) => {
// objects should be:
// [
// {value: 1, new_value: 5},
// {value: 2, new_value: 10},
// {value: 3, new_value: 15}
// ]
});
I know it's possible by nesting multiple observables, concatenating them, and subscribing, but I find it messy. I haven't found an existing observable function provided by the RxJS library, so I'm turning to you guys for help!
Do you have any suggestions or solutions for me?
Thank you!