Hal*_* A. 14 javascript throttling node.js promise es6-promise
我有几个项目需要查询第 3 方 API,并且该 API 的调用限制为每秒 5 次调用。我需要以某种方式将我对 API 的调用限制为每秒最多 5 次调用。
到目前为止,我只使用Promise.all()了一系列 promise,其中每个 promise 向 API 发送一个请求,并在 API 以 HTTP 状态代码响应时进行解析,200并在它以其他状态代码响应时拒绝。但是,当数组中有 5 个以上的项目时,我可能会面临Promise.all()拒绝的风险。
如何将Promise.all()呼叫限制为每秒 5 个呼叫?
我希望这会帮助你。
还要说的是,这将用于Promise.all解决所有请求,如果您有大量查询,这将等待所有请求都解决,并可能导致您的代码等待很多时间以获取所有响应。而且如果其中一个请求被拒绝,Promise.all将拒绝。
我建议,如果你并不需要所有的结果一起最好是用别的东西像lodash防抖动或油门或处置该框架。
let items = [
    {name: 'item1'}, 
    {name: 'item2'}, 
    {name: 'item3'}, 
    {name: 'item4'}, 
    {name: 'item5'}, 
    {name: 'item6'}
];
// This is the api request that you send and return a promise
function apiCall(item) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(item.name), 1000);
  })
}
new Promise((resolve) => {
  let results = [];
  function sendReq (itemsList, iterate, apiCall) {
    setTimeout(() => {
      // slice itemsList to send request according to the api limit
      let slicedArray = itemsList.slice(iterate * 5, (iterate * 5 + 5));
      result = slicedArray.map(item => apiCall(item));
      results = [...results, ...result];
      // This will resolve the promise when reaches to the last iteration
      if (iterate === Math.ceil(items.length / 5) - 1) {
          resolve(results);
      }
    }, (1000 * iterate)); // every 1000ms runs (api limit of one second)
  }
  // This will make iteration to split array (requests) to chunks of five items 
  for (i = 0; i < Math.ceil(items.length / 5); i++) {
    sendReq(items, i, apiCall);
  }
}).then(Promise.all.bind(Promise)).then(console.log);
// Use Promise.all to wait for all requests to resolve
// To use it this way binding is required
如果您不太担心按顺序解决承诺,您可以在 bluebird 中使用并发选项。
下面将一次只处理 5 个查询。
const Promise = require('bluebird');
const buildQueries = (count) => {
  let queries = [];
  for(let i = 0; i < count; i++) {
    queries.push({user: i});
  };
  return queries;
};
const apiCall = (item) => {
  return new Promise(async (resolve, reject) => {
    await Promise.delay(1000);
    resolve(item.user);
  });
};
const queries = buildQueries(20);
Promise.map(queries, async query => {
  console.log( await apiCall(query) );
}, {concurrency: 5});
小智 5
在没有库的情况下使用 ES6
export async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}
export function split(arr, n) {
  var res = [];
  while (arr.length) {
    res.push(arr.splice(0, n));
  }
  return res;
}
export const delayMS = (t = 200) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(t);
    }, t);
  });
};
export const throttledPromises = (
  asyncFunction,
  items = [],
  batchSize = 1,
  delay = 0
) => {
  return new Promise(async (resolve, reject) => {
    const output = [];
    const batches= split(items, batchSize);
    await asyncForEach(batches, async (batch) => {
      const promises = batch.map(asyncFunction).map(p => p.catch(reject));
      const results = await Promise.all(promises);
      output.push(...results);
      await delayMS(delay);
    });
    resolve(output);
  });
};
| 归档时间: | 
 | 
| 查看次数: | 10261 次 | 
| 最近记录: |