Limiting queries using async modules and queries

I combine async and request for asynchronous api request and speed limit.

Here is my code

var requestApi = function(data){ request(data.url, function (error, response, body) { console.log(body); }); }; async.forEachLimit(data, 5, requestApi, function(err){ // do some error handling. }); 

The data contains all the URLs I am referring to. I limit the number of simultaneous requests to 5 using the forEachLimit method. This code makes the first request 5, and then stops.

Asynchronous docs say: "The iterator is passed a callback that should be called after it is completed." But I don’t understand this, what should I do to report that the request is complete?

+4
source share
1 answer

First, you must add a callback to your iterator function:

 var requestApi = function(data, next){ request(data.url, function (error, response, body) { console.log(body); next(error); }); }; 

next(); or next(null); tells Async that all processing is complete. next(error); indicates an error (if error not null ).

After processing all the requests, Async calls the callback function with err == null :

 async.forEachLimit(data, 5, requestApi, function(err){ // err contains the first error or null if (err) throw err; console.log('All requests processed!'); }); 

After receiving the first error or after successfully completing all asynchronous calls, its callback immediately succeeds.

+5
source

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


All Articles