Search code examples
javascriptnode.jsstreamhighland.js

Slowing stream down to one chunk per second


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.


Solution

  • I'd suggest mapping the chunks to delayed streams and sequencing them:

    var _ = require('highland');
    
    function delay(x) {
        return _(function (push, next) {
            setTimeout(function () {
                push(null, x);
                push(null, _.nil);
            }, 1000);
        });
    }
    
    _([1, 2, 3, 4]).map(delay).series().each(_.log);
    

    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 :)