简体   繁体   中英

Wait until all request calls are returned before continuing

I have a program which runs does this:

async function run() {

  for (i of object) {
   request("https://www." + i + ".mydomain.com")
   await wait(250) // synchronous, to prevent API spam
  }

  // HERE
  moveOn()
}

How do I wait until I have gotten a response from all requests before running moveOn()?

Use an http request library that works with promises such as got() :

const got = require('got');

async function run() {

  let promises = [];
  for (let i of object) {
    promises.push(got("https://www." + i + ".mydomain.com"));
    await wait(250) // slight delay, to prevent API spam
  }
  let results = await Promise.all(promises);

  // HERE
  moveOn()
}

A couple other relevant points here.

  1. The request() library has been deprecated and is not recommended for new code.
  2. There is a list of recommended alternative libraries that are all promise-based here .
  3. My personal favorite from that list is got() and it supports most of the options that the request() library does, but wraps things in a bit easier to consume promise-based API (in my opinion) and is well supporting going forward.
  4. A hard-coded 250ms wait between requests is a bit brute force. It would be better to understand what the actual limitations are for the target host and code to those actual limitations which may be something like a max of n requests/sec or no more than n requests in flight at the same time or something like that and there are packages out there to help you implement code to address whatever those limitations are.
  5. You also need to consciously decide what the plan is if there's an error in one of the requests. The structure shown here will abort the run() function is any one request has an error. If you want to do something differently, you need to capture an error from await got() with try/catch and then handle the error in the catch block.

You can use axios() or got() and etc. And please try like this:

async function run() {
  let count = 0;

  for (i of object) {
   axios("https://www." + i + ".mydomain.com")
    .then(res => {
     count++;
     // Your code    
    })
    .catch(err => {
     count++;
    });

   await wait(250) // synchronous, to prevent API spam
  }

  // HERE
  setInterval(() => {
   if (count === object.length) {
    count = 0;
    moveOn()
   }
  }, 250);
}

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