I have a highland stream that is reading a file line by line, and I want to slow it down to one chunk per second. I have looked through the docs, and the only functions I found were throttle() and debounce(). Both of those drop values. I need to keep all my values and just slow down the rate.
Slowing stream down to one chunk per second
387 Views Asked by giodamelio At
2
There are 2 best solutions below
0
On
This is the same version of the function from Caolan but configurable. I made another version for myself that skips the delay on the first element.
var _ = require('highland');
function delay(delayMs) {
return function(x) {
return _(function(push, next) {
return setTimeout((function() {
push(null, x);
return push(null, _.nil);
}), delayMs);
});
};
}
_([1, 2, 3, 4]).map(delay(1000)).series().each(_.log);
I'd suggest mapping the chunks to delayed streams and sequencing them:
The delay function used here seems fairly easy to generalise, so if you're interested in sending a pull request on this I'd be happy to review it :)