我正在尝试异步抓取网站,但一次不执行500个请求,但一次不执行3个请求

module.exports.siteRequest = function(n,userInput) {
  payload["numb"]=n
  axios.post(URL, payload, { headers: headers }).then(res => {
      console.log("id",id)
      console.log(res.data)
    })
    .catch(e => {
      console.log("err");
    });
};


async.timesLimit(500,2, function(n, next) {
          siteRequest(n,data)
        });

最佳答案

您需要在.next中调用timesLimit函数,以便向timesLimit迭代器发出信号,要求下一个项目。例如,以下代码将总共发出10个请求,一次最多发出2个请求:



const makePromise = () => new Promise(res => setTimeout(res, 500)).then(() => {
  console.log('resolving');
});
function siteRequest() {
  console.log('initializing');
  return makePromise();
};


async.timesLimit(10,2, function(n, next) {
  siteRequest().then(next);
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/async/2.6.1/async.min.js"></script>





对于您的代码,请确保也return Promise返回的axios,以便可以在timesLimit中将其链接起来。您还可以使用点表示法,在可能的情况下,它比括号表示法更可取:

module.exports.siteRequest = function(n,userInput) {
  payload.numb = n
  return axios.post(URL, payload, { headers: headers }).then(res => {
    console.log("id",id)
    console.log(res.data)
  })
    .catch(e => {
    console.log("err");
  });
};
async.timesLimit(500,2, function(n, next) {
  siteRequest(n,data).then(next);
});

关于javascript - javascript异步帮助async.timesLimit,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52359729/

10-09 08:40