我是反应式编程的新手,试图在这里理解这个想法。我遇到了一个问题here似乎已经解决了,但是给我留下了更多问题,其中一个是过滤器。这是我从olsn的回答中借来的一些代码,
function myLoop(item_array) {
return Observable.from(item_array)
// if you don't mind the execution-order you can use "mergeMap" instead of "concatMap"
.concatMap(item => configHardwareMock(item)
.switchMap(resultId => Observable.interval(500)
.do(() => console.info("Checking status of: " + resultId))
.switchMap(() => intervalCheckingStatus(resultId))
.filter(status => Boolean(status)) // your logic if the status is valid, currently just a boolean-cast
.take(1) // and complete after 1 value was valid
.mapTo(item) // map back to "item" so we can notify the subscriber (this is optional I guess and depends on if you want this feature or not)
)
);
}
//an example of such boolean from olsn
function intervalCheckingStatus(resultId) {
if (Math.random() < .4) {
return Observable.of(false);
}
return Observable.of(true);
}
我想我理解了一些工具语句,例如concatMap
,take
,from
等等。现在它是.filter(status => Boolean(status))
,所以如果我必须放另一个在这个布尔函数中严重的REST API请求/通信,那里可能有另一个subscrible()
代码。它会破坏这样的过滤器吗?事物(事件)将以什么顺序运行?这个硬件通信是否正常,或者我应该让它不是异步(因为它是一个不那么强大的硬件)?
答案 0 :(得分:0)
要回答您的问题 - 您不能在filter()中添加另一个subscribe()。 (订阅将运行但不会对过滤器产生任何影响)
如果要执行另一个异步功能(例如REST API调用)以确定要过滤的值,则需要使用.flatmap()
运算符将其链接。