I'm trying to create a rate limiting when accessing an external API, using Bacon.JS
The rate limiting works fine, using bufferWithCount and bufferingThrottle but i would like to get the results when everything is flatmapped, not each batch at a time.
I've tries onEnd but it does not seem to be triggered.
Here's a 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);
}
You can use fold
to combine the results and .end()
to cause the Bus to 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.