Answer the question
In order to leave comments, you need to log in
RxJs: conditional mergeMap in pipe?
I have this amazing code:
this.myApi.getSomething(id).pipe(
shop.isNew
? (
map(tags => ({ ...updatedShop, tags })),
mergeMap(() =>
this.myApi.addTags(updatedShop.id, shop.tags).pipe(
map(() => {
return {
...updatedShop,
tags: shop.tags
};
})
)
)
)
: map(tags => ({ ...updatedShop, tags }))
)
Answer the question
In order to leave comments, you need to log in
// Да, сначала мы делаем map, потому что он делается полюбому:
let doSomething$ = this.myApi.getSomething(id).pipe(
map(tags => ({ ...updatedShop, tags }))
)
// А затем мы делаем mergeMap если понадобится:
if (shop.isNew) {
doSomething$ = doSomething$.pipe(
mergeMap(() =>
this.myApi.addTags(updatedShop.id, shop.tags).pipe(
map(() => {
return {
...updatedShop,
tags: shop.tags
};
})
)
)
)
}
// ну и затем подписываемся на это дело чтоб оно начало работать:
doSomething$.subscribe();
this.myApi.getSomething(id).pipe(
map(tags => ({ ...updatedShop, tags })),
mergeMap((tags) => {
if (!shop.isNew) {
return of(tags);
}
return this.myApi.addTags(updatedShop.id, shop.tags).pipe(
map(() => {
return {
...updatedShop,
tags: shop.tags
};
})
)
}),
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question