Skip to content

whilenot-dev/promises-batched

Repository files navigation

promises-batched

Iterate through promises in a batched manner.

This package implements the ECMAScript functions Promise.all, Promise.allSettled, and Promise.any as batched variants with 0 dependencies.

The type signature adheres to the TypeScript lib implementation of the ECMAScript functions.

Install

Install with your package manager of choice:

$ npm i promises-batched

Usage

Since Promises are executed eagerly we need to provide a lazy dispatch behavior by wrapping each Promise expression in a function.

promiseAllBatched

import { promiseAllBatched } from "promises-batched";

const promiseFn1 = () => Promise.resolve(3);
const promiseFn2 = () => 42;
const promiseFn3 = () => new Promise<string>((resolve, _reject) => setTimeout(resolve, 100, "foo"));

const promiseFns = [promiseFn1, promiseFn2, promiseFn3];

promiseAllBatched(2, promiseFns).then((values) => {
  console.log(values);
});

// Expected output: Array [3, 42, "foo"]

(Example from MDN)

promiseAllSettledBatched

import { promiseAllSettledBatched } from "promises-batched";

const promiseFn1 = () => Promise.resolve(3);
const promiseFn2 = () => new Promise<string>((_resolve, reject) => setTimeout(reject, 100, "foo"));

const promiseFns = [promiseFn1, promiseFn2];

promiseAllSettledBatched(1, promiseFns).then((results) => {
  results.forEach((result) => {
    console.log(result.status);
  });
});

// Expected output:
// "fulfilled"
// "rejected"

(Example from MDN)

promiseAnyBatched

import { promiseAnyBatched } from "promises-batched";

const promiseFn1 = () => Promise.reject(new Error("error"));
const promiseFn2 = () => new Promise<string>((resolve) => setTimeout(resolve, 100, "quick"));
const promiseFn3 = () => new Promise<string>((resolve) => setTimeout(resolve, 500, "slow"));

const promiseFns = [promiseFn1, promiseFn2, promiseFn3];

promiseAnyBatched(2, promiseFns).then((value) => {
  console.log(value);
});

// Expected output: "quick"

(Example from MDN)

Alternatives

  • p-limit - Run multiple promise-returning & async functions with limited concurrency

License

MIT - wh!le (whilenot-dev), see LICENSE

About

Iterate through promises in a batched manner

Resources

License

Stars

Watchers

Forks