简体   繁体   中英

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

I'd like to be able to throttle the calls to getPagerank() to one per second. I've tried various things but can't 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);
});

You can use .ratelimit()

eg this will limit the stream to processing one item of the array per one second

var _ = require('highland');

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM