简体   繁体   English

等到所有请求调用都返回后再继续

[英]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()?在运行 moveOn() 之前,如何等到我得到所有请求的响应?

Use an http request library that works with promises such as got() :使用 http 请求库,该库与诸如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. request()库已被弃用,不推荐用于新代码。
  2. There is a list of recommended alternative libraries that are all promise-based here .这里有一个推荐的替代库列表,它们都是基于Promise的。
  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.该列表中我个人最喜欢的是got() ,它支持request()库所做的大多数选项,但更容易使用基于 Promise 的 API (在我看来)并且很好地支持前进。
  4. A hard-coded 250ms wait between requests is a bit brute force.请求之间的硬编码 250 毫秒等待有点蛮力。 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.最好了解目标主机的实际限制和代码对这些实际限制的实际限制,这些实际限制可能类似于最多 n 个请求/秒或同时在飞行中不超过 n 个请求或类似的东西,并且有一些软件包可以帮助您实现代码来解决这些限制。
  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.此处显示的结构将中止run() function 是任何一个请求都有错误。 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.如果你想做一些不同的事情,你需要使用try/catchawait got()捕获错误,然后在catch块中处理错误。

You can use axios() or got() and etc. And please try like this:您可以使用axios()got()等。请尝试这样:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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