我在尝试着repeat
根据返回的值进行承诺调用condition
场地。以下块不起作用,因为v
未定义且随机抛出TypeError: Cannot read property 'condition' of undefined
console.log 的 o/p 是{ Items: [ 1, 2, 3, 4, 5 ], condition: 5, time: 1513827310333 }
JSFiddle https://jsfiddle.net/nilobarp/3sx2gh2c/
const source = Rx.Observable.fromPromise(
Promise.resolve({
Items: [1, 2, 3, 4, 5],
condition: Math.floor(Math.random() * 10),
time: +new Date()
})
);
source
.map(val => val)
.repeatWhen(val => {
return val.map(v => { // v is undefined
if (v.condition > 0) {
return Rx.Observable.of(v);
} else {
return Rx.Observable.empty();
}
});
})
.finally(() => {
done();
})
.subscribe(val => console.log(val));
让它与外部标志一起工作,并为每次重复评估源。
const source = Rx.Observable.defer(() =>
Promise.resolve({
Items: [1, 2, 3, 4, 5],
condition: Math.floor(Math.random() * 10),
time: +new Date()
})
);
let condition = false;
source
.repeatWhen(notifications => {
return notifications
.scan(() => {
return condition;
}, false)
.delay(100)
.takeWhile(() => {
return condition;
});
})
.do(x => (condition = x.condition !== 0))
.finally(console.log("done"))
.subscribe(console.log);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.5/Rx.min.js"></script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)