Skip to content

Instantly share code, notes, and snippets.

@fabienjuif
Last active October 5, 2025 08:03
Show Gist options
  • Select an option

  • Save fabienjuif/6a91e77226cbb0bce4c981ea8dc51ec5 to your computer and use it in GitHub Desktop.

Select an option

Save fabienjuif/6a91e77226cbb0bce4c981ea8dc51ec5 to your computer and use it in GitHub Desktop.
Javascript / Batch / Promises

Snippet

const Promise = require('bluebird');

const batch = async (args, callback, options = {}) => {
  const { max = 2 } = options;
  const results = [];

  let running = args.slice(0, max).map(a => callback(...a));
  let picked = running.length;

  while (running.length > 0) {
    results.push(await Promise.race(running));

    const stillRunning = running.filter(p => p.isPending());
    const done = running.length - stillRunning.length;
    const next = args.slice(picked, picked + Math.min(done, max)).map(a => callback(...a));

    picked += next.length;

    running = stillRunning.concat(next);
  }

  return results;
};

Example usage

const impl = (f) => {
  console.log('starting', f);

  return new Promise((resolve, reject) => setTimeout(() => {
    console.log(f);
    if (f === "file2") reject("oups");
    else resolve();
  }, 1000));
};

batch([["file1"], ["file2"], ["file3"]], impl, { max: 2 });

Dependencies

  • bluebird
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment