当 A 或 B 有 rxjs 的有效响应时如何做 C?
how to do C when either A or B has a valid response by rxjs?
一旦我收到状态为'success'的A的响应,然后执行C。
一旦我得到 B 的响应,其代码为 0,然后执行 C.
如果我使用combineLastest
combineLatest(A,B)
.pipe(
filter( ([a,b])=> {a.status == 'success' || b.code == 0} )
)
.subscribe(()=>doC());
会不会在 B return 一个无效的响应(代码不为 0)时,但是因为 A 之前有一个有效的响应,所以它仍然通过 filter
并调用 doC()?这与我的目标背道而驰。
如果可以,我应该使用什么运算符?
非常感谢
(这里我不关心使用A或B的响应数据,但如果我需要在doC
中使用它,我应该使用什么运算符?)
如果我对问题的理解正确,我会这样进行(代码解释见注释)
// you create 2 separate Observables, one with source A and one with souce B,
// representing the streams whose notifications trigger doC()
const source_A = A.pipe(
filter(a => a.status == 'success')
);
const source_B = B.pipe(
filter(b => b.code == 0})
);
// then you merge these 2 Observables to create a new Observable which notifies when any of these Observables notifies
merge(source_A, source_B).pipe(
// then you perform doC() when any notification arrives
// here I use the tap Operator but you can also insert this logic
// into the subscribe parameter
tap(value => {
// here value is either 'success' or 0 depending on whether A or B
// are the origin of the notification
doC()
})
)
// eventually you subscribe
.subscribe()
一旦我收到状态为'success'的A的响应,然后执行C。 一旦我得到 B 的响应,其代码为 0,然后执行 C.
如果我使用combineLastest
combineLatest(A,B)
.pipe(
filter( ([a,b])=> {a.status == 'success' || b.code == 0} )
)
.subscribe(()=>doC());
会不会在 B return 一个无效的响应(代码不为 0)时,但是因为 A 之前有一个有效的响应,所以它仍然通过 filter
并调用 doC()?这与我的目标背道而驰。
如果可以,我应该使用什么运算符?
非常感谢
(这里我不关心使用A或B的响应数据,但如果我需要在doC
中使用它,我应该使用什么运算符?)
如果我对问题的理解正确,我会这样进行(代码解释见注释)
// you create 2 separate Observables, one with source A and one with souce B,
// representing the streams whose notifications trigger doC()
const source_A = A.pipe(
filter(a => a.status == 'success')
);
const source_B = B.pipe(
filter(b => b.code == 0})
);
// then you merge these 2 Observables to create a new Observable which notifies when any of these Observables notifies
merge(source_A, source_B).pipe(
// then you perform doC() when any notification arrives
// here I use the tap Operator but you can also insert this logic
// into the subscribe parameter
tap(value => {
// here value is either 'success' or 0 depending on whether A or B
// are the origin of the notification
doC()
})
)
// eventually you subscribe
.subscribe()