3

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.

giodamelio
  • 4,885
  • 14
  • 39
  • 70

2 Answers2

3

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

Caolan
  • 3,819
  • 1
  • 15
  • 8
0

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);
Michael Connor
  • 3,752
  • 21
  • 21