我是反应式编程的新手,试图理解这里的想法。我在这里遇到了一个问题,似乎已经解决了,但是给我留下了更多的问题,其中一个是过滤器。这是我从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()
在那里的代码。它会破坏这种过滤器吗?事物(事件)以什么顺序运行?这是正常的硬件通信,还是我应该使其不异步(因为它是不是那么强大的硬件)?
要回答您的问题-您不能在filter()中放置另一个subscription()。(订阅将运行,但不会对过滤器产生任何影响)
如果要执行另一个异步功能(例如REST API调用)以确定要过滤的值,则需要将其与.flatmap()
运算符链接在一起。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句