0.1.1 • Published 4 years ago

@ct0r/promise-queue v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

ct0r/promise-queue

Minimalistic promise queue.

Installation

npm install @ct0r/promise-queue

Usage

const queue = require("@ct0r/promise-queue");

const enqueue = queue({ concurrency: 2 });

Promise.all([
  enqueue(() => fetch("https://example.com/1")),
  enqueue(() => fetch("https://example.com/2")),
]);

Arguments can be passed separately:

Promise.all([
  enqueue(fetch, "https://example.com/1"),
  enqueue(fetch, "https://example.com/2"),
]);

Can be used to add concurrency support to existing async function:

const enqueueFetch = queue({ fn: fetch });

Promise.all([
  enqueueFetch("https://example.com/1"),
  enqueueFetch("https://example.com/2"),
]);

Perfect in combination with iterables:

Promise.all(
  ["https://example.com/1", "https://example.com/2"].map(enqueueFetch)
);

API

queue({ concurrency = 1, fn })

Sets concurrency level and returns enqueue function.

enqueue(...args)

Returned by queue if fn is provided.

enqueue(fn, ...args)

Returned by queue if fn is not provided.