我试图在访问外部API时使用Bacon.JS创建一个速率限制
使用bufferWithCount和bufferingThrottle可以很好地限制速率,但是当所有的东西都被平面图时,我想得到结果,而不是一次映射每一批。
我尝试过onEnd,但它似乎没有触发。
这里有一个小提琴:http://jsfiddle.net/9324jyLr/1/
var stream = new Bacon.Bus();
stream
.bufferWithCount(2)
.bufferingThrottle(1000)
.flatMap(batch => {
batch = batch.map(x => x*2); //this should be an async API call returning Bacon.fromPromise(...)
return Bacon.fromArray(batch);
})
// .bufferWithTime(1000)//one thang per interval
.onValue(val => $('#log').append(val));
for (var i=0; i<10; i++) {
stream.push(i);
}发布于 2016-03-07 12:32:56
您可以使用fold组合结果,使用.end()使总线结束。
stream
.bufferWithCount(2)
.bufferingThrottle(1000)
.flatMap(batch => {
batch = batch.map(x => x*2); //this should be an async op
return Bacon.fromArray(batch);
})
.fold([], (arr, val) => { return arr.concat(val) })
// .bufferWithTime(1000)//one thang per interval
.onValue(val => $('#log').append(val+"\n"));
for (var i=0; i<10; i++) {
stream.push(i);
}
stream.end()http://jsfiddle.net/jdr9wuzy/
https://stackoverflow.com/questions/35839248
复制相似问题