byCoder
byCoder

Reputation: 9184

RxJS: skip conditional mergeMap

I have such service method:

updateUser(article) {
        this.httpClient
            .put(`articles/${article.id}`, article)
            .pipe(
                map(res => res.response),
                mergeMap(updatedArticle =>
                    this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
                        map(tags => ({ ...updatedArticle, tags })),
                        article.articleType === 'news'
                            ? mergeMap(() =>
                                  this.articlesTagsAPI.someOtherMethod(updatedArticle.id, article.tags).pipe(
                                      map(() => {
                                          return {
                                              ...updatedArticle,
                                              tags: article.tags
                                          };
                                      })
                                  )
                              )
                            : null
                    )
                )
            );
}

as you can see - I'm trying to add a conditional mergeMap (if condition -> call else one more method) is it possible to do somehow?

because returning null - isn't the best idea

something like this should happen if conditional is invalid:

updateUser(article) {
            this.httpClient
                .put(`articles/${article.id}`, article)
                .pipe(
                    map(res => res.response),
                    mergeMap(updatedArticle =>
                        this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
                            map(tags => ({ ...updatedArticle, tags }))
                        )
                    )
                );
    }

Upvotes: 0

Views: 2736

Answers (2)

Abhishek Singh
Abhishek Singh

Reputation: 1

Try this:

updateUser(article) {

  this.httpClient
        .put(`articles/${article.id}`, article)
        
       .pipe(
            map(res => res.response),
            

                mergeMap(() =>       
                 this.articlesTagsAPI.someOtherMethod(updatedArticle.id, 
                           article.tags).pipe(
                                  map(() => {
                                     if(article.articleType === 'news'){
                                      return {
                                          ...updatedArticle,
                                          tags: article.tags
                                      };
                                     }else{ return of([])}
                                  })
                              )
                          )
                        
                )
            )
        );

}

Upvotes: 0

rguerin
rguerin

Reputation: 2128

As stated in this very useful article, the proper way of doing this would be to add the of instruction to always return an Observable its value inside your final subscribe.

Try this:

updateUser(article) {

    this.httpClient
        .put(`articles/${article.id}`, article)
        .pipe(
            map(res => res.response),
            mergeMap(updatedArticle =>
                this.articlesTagsAPI.someMethod(updatedArticle.id).pipe(
                    map(tags => ({ ...updatedArticle, tags })),
                    article.articleType === 'news'
                        ? mergeMap(() =>
                                this.articlesTagsAPI.someOtherMethod(updatedArticle.id, article.tags).pipe(
                                    map(() => {
                                        return {
                                            ...updatedArticle,
                                            tags: article.tags
                                        };
                                    })
                                )
                            )
                        : of(null)
                )
            )
        );
}

Upvotes: 2

Related Questions