angular - Condition the execution of a flatmap in rxjs - angular 5
问题描述
In rxjs I want to make a condition over multiple flatmap so I don't execute the second and third flatmap and I go directly to the subscribe :
this.authenticationAPI.getUser()
.flatmap(response1 => if(response1 != null) // I want to go directly to subscribe
.flatmap(response2 => return observable2(response2))
.flatmap(response3 => return observable3(response3))
.subscribe(response 4 => console.log(response4));
I thought about passing null observables to the second and third flatmap but is there any other solution?
解决方案
您应该分为 2 种情况:使用filter
. 这种方式更具声明性,更易于阅读
const response$ = this.authenticationAPI.getUser();
// failure
response$
.filter((res) => !res)
.subscribe(() => {
console.log('Unauthentication')
});
// success
response$
.filter((res) => res)
.flatmap(response2 => return observable2(response2))
.flatmap(response3 => return observable3(response3))
.subscribe(response 4 => console.log(response4));
推荐阅读
- xcode - 在 React Native 项目中安装 Cocoapods 时出错
- c - 是否有充分的理由来检查布尔值是否相等?
- node.js - 如何防止 Mongoose findOneAndUpdate 自动添加 ObjectId
- java - 为什么 Hibernate 延迟加载在 Kotlin 中的行为不同?
- python - Pandas:如何绘制具有多索引且索引中只有一个值的图形
- java - Java持久性:插入应用层的数据在junit类中的DB调用中不可见
- python - 根据坐标数据加入熊猫数据框
- python - 如何在pygame中连续播放声音样本?
- javascript - 如何限制类型脚本中的数组大小?
- javascript - 使用 SMTP.JS 发送多个附件