0.6.2 • Published 4 years ago

qler v0.6.2

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

Qler

NPM JavaScript Style Guide

Excruciatingly simple synchronous queuing for node, with concurrency support. It provides similar functionality to p-queue, with the ability to lock concurrency based on a key.

Used in production at https://wellpaid.io.

Installation

npm install qler
## or
yarn add qler

API

  • queue(fn, key) - queue a promise that returns a promise. Will never run two fns with the same key.
  • cancel() - will cancel all remaining queue items and reject any remaining queue promises.
  • wait() - wait for all previously queued promises to complete. Returns a promise.

Usage

Basic

Execute two functions in sequence, without blocking main thread.

import Qler from "qler";

const myQueue = Qler();

myQueue
  .queue(async () => await sleep(2)) // Wait for 2 seconds
  .then(() => console.log(`Function 1 executed after 2 seconds!`));

myQueue
  .queue(async () => await sleep(2)) // Wait for 2 seconds
  .then(() => console.log(`Function 2 executed after 4 seconds!`));

With concurrency

import Qler from "qler";

const myQueue = Qler(2);

myQueue
  .queue(async () => await sleep(2)) // Wait for 2 seconds
  .then(() => console.log(`Function 1 executed after 2 seconds!`));

myQueue
  .queue(async () => await sleep(2)) // Wait for 2 seconds
  .then(() => console.log(`Function 2 executed after 2 seconds!`));

myQueue
  .queue(async () => await sleep(2)) // Wait for 2 seconds
  .then(() => console.log(`Function 3 executed after 4 seconds!`));

With keyed concurrency

Keyed concurrency allows you to limit concurrency to certain function calls. If two or more queued function calls share the same key, they won't be run concurrently.

import Qler from "qler";

const myQueue = Qler(2);

myQueue
  .queue(async () => await sleep(2), "foo") // Wait for 2 seconds and key on 'foo'
  .then(() => console.log(`Function 1 executed after 2 seconds!`));

myQueue
  .queue(async () => await sleep(2), "foo") // Wait for 2 seconds and key on 'foo'
  .then(() => console.log(`Function 2 executed after 4 seconds!`));

myQueue
  .queue(async () => await sleep(2), "bar") // Wait for 2 seconds and key on 'bar'
  .then(() => console.log(`Function 3 executed after 2 seconds!`));

License

MIT © Chris Villa

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago