How to queue http get requests in Nodejs in order to control their rate?

前端 未结 3 1115
后悔当初
后悔当初 2021-02-20 16:27

I have a NodeJS app which sends HTTP get requests from various places in the code, some are even dependent (sending a request, waiting for a reply, processing it and based on re

3条回答
  •  不要未来只要你来
    2021-02-20 16:40

    I would use Deferreds and return one for every queued request. You can then add succeed/fail callbacks to the deferred promise after it has been queued.

    var deferred = queue.add('http://example.com/something');
    deferred.fail(function(error) { /* handle failure */ });
    deferred.done(function(response) { /* handle response */ });
    

    You can hold a [ url, deferred ] pairs in your queue, and each time you dequeue a URL you'll also have the Deferred that goes with it, which you can resolve or fail after you process the request.

提交回复
热议问题