我正在尝试使用 Bacon.JS 访问外部 API 时创建速率限制
使用 bufferWithCount 和 bufferingThrottle 的速率限制工作正常,但我希望在所有内容都平面映射时获得结果,而不是一次每个批处理。
我尝试过 onEnd 但它似乎没有被触发。
这是一个 fiddle :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);
}
最佳答案
您可以使用 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()
关于javascript - 使用 Bacon.JS 进行速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35839248/