npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qask

v1.3.0

Published

Queue tasks to run in intervals sequentially with fixed concurrency.

Downloads

16

Readme

qask

Queue tasks to run in intervals sequentially with fixed concurrency. Useful for ratelimiting API requests and the such.

Small and has no dependencies, for node and the browser.

Usage

import { Queue } from "qask";

async function main () {

	const queue = Queue({ interval: 1000, concurrency: 1, autoStart: true });

	// elsewhere
	const companies = await queue.add(
		() => fetch(`https://example.com/api/v1/companies`));
	
	// elsewhere
	const users = await queue.add(
		() => fetch(`https://example.com/api/v1/users`));
	
	// elsewhere
	const clicks = await queue.add(
		() => fetch(`https://example.com/api/v1/clicks`));
	
	// elsewhere
	const reads = await queue.add(
		() => fetch(`https://example.com/api/v1/reads`));

	// All these functions are queued to run at a maximum of 1 per second
}

main();

API

Queue ({ interval, concurrency, autoStart })

Queue factory. Creates a new queue instance.

Params

  • interval (number, default: 0) milliseconds to wait after each cycle
  • concurrency (number, default: 1) number of items to process each cycle
  • autoStart (boolean, default: false) whether to start queue immediately on first push

queue.add (fn)

Add a function to the queue and returns a Promise of return type of fn. Call without params to create a wait function that will resolve when picked from queue if you simply want to lock and take control.

Note: if your function throws an error, .add() will reject, but queue will continue to process. Remember to handle your errors with try await / catch or .catch().

queue.addAll ([...fns])

Adds all functions to queue. If any of the functions throw or reject, .addAll() will reject. However, if all functions resolve, the returned Promise will resolve to an array of values. All other semantics are identical to .add().

queue.start ()

Starts the queue. No-op if queue is already running.

queue.pause ()

Pauses queue execution. Any processes already started will continue, new tasks will not be picked until .start() is called. No-op if queue is already paused.

queue.clear ()

Clears the queue. Queue is not paused, and will continue to try and pick tasks.

queue.cancel ()

Clears the queue and stops taking tasks. Any processes already started will continue.

queue.on (event, listener)

Adds an eventlistener to specified event.

Params

  • event (string) One of "start", "next", "drain", "pause", "clear", "cancel"
  • listener (function) Takes a context object, refer to TS types.

queue.off (event, listener)

Removes an eventlistener to specified event.

Params

  • event (string) One of "start", "next", "drain", "pause", "clear", "cancel"
  • listener (function) Takes a context object, refer to TS types.

queue.interval

(number)

Interval passed via Queue factory. Assign to this prop to modify.

queue.concurrency

(number)

Concurrency passed via Queue factory. Assign to this prop to modify.

queue.size (getter)

(number)

Gets the remaining size of the queue at any point. Cannot be assigned to.

queue.pending (getter)

(number)

Gets the tasks that were started from the queue but pending completion. Cannot be assigned to.

queue.hasStarted (getter)

(boolean)

Returns true if queue has started. Cannot be assigned to.

queue.isEmpty (getter)

(boolean)

Returns true if queue is empty. Cannot be assigned to.

queue.isPaused (getter)

(boolean)

Returns true if queue is paused. Cannot be assigned to.

Credits

Originally started as a gist based on a question by @darvesh with inputs from @TRGWII.