Reputation: 454
I use from() with pipe and concatMap and i would like to have access to the object in concatMap because i need it for doing a mapping after.
from(objects)
.pipe(
concatMap(object => // i need to have access to object in subscribe
defer(() =>
this.service.getObjectInfors(object.id)
)
)
)
.subscribe(objectInfos => {
if (objectInfos) {
this.objectsTab.push(
this.mappingObject(
object, // i need object in this mapping
objectInfos
)
);
}
});
Is it possible to do that ? Is there another way which can help me doing that ? Thanks
Upvotes: 0
Views: 884
Reputation: 8062
If you want to avoid turning an array into a stream, you can instead merge an array of streams.
You can use the closure from array.map to map each object into the results of its stream.
const infoObjectsCalls = objects.map(object =>
defer(() => this.service.getObjectInfors(object.id)).pipe(
filter(objectInfos => objectInfos != null),
map(objectInfos => ({
object,
objectInfos
}))
)
);
merge(...infoObjectsCalls).subscribe(mappingObject =>
this.objectsTab.push(
this.mappingObject(mappingObject)
)
);
Upvotes: 1
Reputation: 31125
You could just pipe in a map
to the inner observable inside the concatMap
and send an object with both values.
Try the following
from(objects).pipe(
concatMap(object =>
defer(() =>
this.service.getObjectInfors(object.id).pipe(
map(objectInfos => ({
obj: object,
infos: objectInfos
}))
)
)
)
)
.subscribe((data: any) => {
// access `data.obj` and `data.infos` here
if (data.infos) {
this.objectsTab.push(
this.mappingObject(
data.obj,
data.infos
)
);
}
});
Upvotes: 2