How can I feed a Highland.js or Node.js stream to one object per second?

I would like to be able to throttle calls getPagerank()to unity per second. I tried different things, but I can not get it to work.

var pagerank = require('pagerank');
var _ = require('highland');

var urls = [
    'google.com',
    'yahoo.com',
    'bing.com'
];

var getPagerank = _.wrapCallback(pagerank);

// I want to throttle calls to getPagerank to 1/sec
var pageRanks = _(urls)
    .map(getPagerank)
    .merge();

pageRanks.toArray(function(arr) {
    console.log(arr);
});
+4
source share
1 answer

you can use .ratelimit()

eg. this limits the flow to processing one element of the array in one second.

var _ = require('highland');

_([1,2,3,4]).ratelimit(1, 1000).map(function(x){
  return String(x);
})
.pipe(process.stdout);
+3
source

Source: https://habr.com/ru/post/1607683/


All Articles