New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

queue-promised

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

queue-promised

Node.js library for Promising response of function from queue.

  • 0.0.30
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

queue-promised

Known Vulnerabilities Build Status npm License: MIT Coverage Status

Library for rate limiting function executions.

You get promise to function that can be executed later, if there are no free workers right now.

Table of contents

Installation

yarn add queue-promised

FAQ

What are workers?

Worker is just a function that is being called as one of the "threads".

This library does not provide any additional multithreading for javascript.

Usage

Wrapper example

This is the main way this library is intended to be used, just for wrapping function you want to rate limit.

const _ = require("lodash");

const queuePromise = require("queue-promised");

const wrapper = queuePromise.wrapper;

const limitedFunction = wrapper((time) => {
	// This is worker functions. It can either return data or Promise
	return new Promise(resolve => {
		setTimeout(() => {
			resolve(time);
		}, time);
	});
}, 100);

// Generate params for 1000 tasks
const times = _.times(1000, () => Math.random() * 2000);

// Run tasks
times.forEach(time => {
	// Run function limitedFunction with time as param
	limitedFunction(time).then(data => {
		console.log(data);
	}).catch((e) => {
		console.error(e.toString());
	});
});

For more info on wrapper function, feel free to read API docs.

Advanced example

If you wish to go a little deeper and to create your own rate limiting logic, this is the way to do it.

For even more details, feel free to read ./src/wrapper/index.js.

const _ = require("lodash");

const queuePromise = require("queue-promised");

const queue = queuePromise.queue;
const worker = queuePromise.worker;

const limitedFunction = (time) => {
	// This is worker functions. It can either return data or Promise
	return new Promise(resolve => {
		setTimeout(() => {
			resolve(time);
		}, time);
	});
};

// Add that function to worker queue 100 times
_.times(100, () => worker("sleeper", limitedFunction));

// Generate params for 1000 tasks
const times = _.times(1000, () => Math.random() * 2000);

// Run tasks
times.forEach(time => {
	// Run function limitedFunction with time as param
	queue.push("sleeper", time).then(data => {
		console.log(data);
	}).catch((e) => {
		console.error(e.toString());
	});
});

Structured advanced example

const queuePromise = require("queue-promised");

const queue = queuePromise.queue;
const worker = queuePromise.worker;

const tester = {
	start: () => {
		// Generate params for tasks
		const times = _.times(1000, () => Math.random() * 2000);

		// Run tasks
		times.forEach(time => {
			queue.push("sleeper", time).then(data => {
				console.log(data);
			}).catch((e) => {
				console.error(e.toString());
			});
		});
	},
	startWorkers: () => {
		// Generate workers
		_.times(100, (id) => worker("sleeper", (time) => {
			// This is worker functions. It can either return data or Promise
			return new Promise(resolve => {
				setTimeout(() => {
					resolve("worker" + id + " - " + time);
				}, time);
			});
		}));
	}
};

// Allocate workers
tester.startWorkers();

// Run tasks
tester.start();

Design decision

Workers initialization

Idea behind task initialization is to register functions to specific queue.

In example, for each worker, I created new function, this is to demonstrate you can for example have multiple different handling functions that send tasks to different external task handler.

Task execution

Task execution is done in first come - first serve manner. When function returns result, it is added to queue of workers.

API docs

You can read API docs.

Authors

FAQs

Package last updated on 22 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc