J
J
jenya77712017-12-31 18:13:19
Node.js
jenya7771, 2017-12-31 18:13:19

How to execute n number of parallel requests using async?

Hello, how to execute the nth number of parallel requests using async, here is an example:

for (var i in data) {

          request({
            url: 'https://google.com',
            agentClass: Agent,
            agentOptions: {
              socksHost: data[i].host,
              socksPort: data[i].port
            }

          }, function(errChek, resChek) {

            if (!errChek) {

              data[i].status = true;
              
            } else {

              data[i].status = false;
              
            }
          })
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Gerasimov, 2017-12-31
@jenya7771

async.times
async.each

async.each(data, (row, cb) => {
  request({
    url: 'https://google.com',
    agentClass: Agent,
    agentOptions: {
      socksHost: row.host,
      socksPort: row.port
    }
  }, (errChek, resChek) => {
    row.status = !errChek;
    return cb(null, row);
  });
}, (err, results) => {
  // complete
});

A
Anton, 2017-12-31
@SPAHI4

await Promise.all(data.map(i => request(i)));

V
vitaliy2, 2017-12-31
@vitaliy2

Either like this: Or like this:

const p1 = callSomeAsyncFunction(),
      p2 = callAnotherAsyncFunction(),
      p3 = callAnotherAsyncFunction();

await p1;
await p2;
await p3;

In the second example, we immediately launched three functions at the same time. Further, when they have already begun work, we expect the completion of all three functions. At the same time, since they are already running properly and performing the necessary actions, there will be no unnecessary delay, because they are already running.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question