Skip to content
本页目录

PromisePool

  • Promise.race + Queue
js
const promisePool = async function (functions, n) {
  const queue = new Set();
  const resolved = [];
  for (const task of functions) {
    const x = task().then((res) => {
      resolved.push(res);
      queue.delete(x);
    });
    queue.add(x);
    if (queue.size >= n) {
      await Promise.race(queue);
    }
  }
  await Promise.allSettled(queue);
  return resolved;
};

const sleep = (t) =>
  new Promise((resolve) =>
    setTimeout(() => {
      resolve(t);
    }, t)
  );

const functions = [() => sleep(300), () => sleep(400), () => sleep(200)];
const n = 5;

promisePool(functions, n).then(console.log);
  • Promise.all + map + shift
js
const promisePool = async function (functions, n) {
  return Promise.all(
    [...new Array(n)].map(async () => {
      while (functions.length > 0) {
        const res = await functions.shift()();
        console.log(res);
      }
    })
  );
};

const sleep = (t) =>
  new Promise((resolve) =>
    setTimeout(() => {
      resolve(t);
    }, t)
  );

const functions = [() => sleep(300), () => sleep(400), () => sleep(200)];
const n = 5;

promisePool(functions, n).then(console.log);

p-limit