# queue-promised

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

Latest version **0.0.31** (published 2026-11-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install queue-promised
pnpm add queue-promised
yarn add queue-promised
bun add queue-promised
```

## Health

**Score 55/100 (C)** — status: active.

Positive: no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.0.31 |
| Published | 2026-11-05 |
| First published | 2019-07-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=14.17 |
| Dependencies | 0 |
| Unpacked size | 19.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | nemanjan00 |
| Maintainers | nemanjan00 |

## Links

- npm: https://www.npmjs.com/package/queue-promised
- Repository: https://github.com/nemanjan00/queue-promised
- Homepage: https://github.com/nemanjan00/queue-promised#readme
- Issues: https://github.com/nemanjan00/queue-promised/issues
- npm.io page: https://npm.io/package/queue-promised

## Recent versions

- 0.0.31 (latest) — 2026-11-05
- 0.0.30 — 2022-11-22
- 0.0.29 — 2022-10-17
- 0.0.28 — 2022-10-17
- 0.0.27 — 2019-09-24
- 0.0.25 — 2019-08-27
- 0.0.24 — 2019-08-27
- 0.0.23 — 2019-08-01
- 0.0.22 — 2019-08-01
- 0.0.21 — 2019-08-01
- 0.0.19 — 2019-08-01
- 0.0.18 — 2019-08-01
- 0.0.17 — 2019-08-01
- 0.0.16 — 2019-08-01
- 0.0.15 — 2019-08-01
- … 13 more at https://npm.io/package/queue-promised/versions

## README

# queue-promised

[![Known Vulnerabilities](https://snyk.io//test/github/nemanjan00/queue-promised/badge.svg?targetFile=package.json)](https://snyk.io//test/github/nemanjan00/queue-promised?targetFile=package.json)
[![Build Status](https://travis-ci.org/nemanjan00/queue-promised.svg?branch=master)](https://travis-ci.org/nemanjan00/queue-promised)
[![npm](https://img.shields.io/npm/dt/queue-promised)](https://www.npmjs.com/package/queue-promised)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Coverage Status](https://coveralls.io/repos/github/nemanjan00/queue-promised/badge.svg?branch=master)](https://coveralls.io/github/nemanjan00/queue-promised?branch=master)

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

<!-- vim-markdown-toc GFM -->

* [Installation](#installation)
* [FAQ](#faq)
	* [What are workers?](#what-are-workers)
* [Usage](#usage)
	* [Wrapper example](#wrapper-example)
	* [Advanced example](#advanced-example)
	* [Structured advanced example](#structured-advanced-example)
* [Design decision](#design-decision)
	* [Workers initialization](#workers-initialization)
	* [Task execution](#task-execution)
	* [API docs](#api-docs)
* [Authors](#authors)

<!-- vim-markdown-toc -->

## Installation

```bash
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. 

```javascript
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](https://github.com/nemanjan00/queue-promised/blob/master/docs/wrapper.md).

### 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`. 

```javascript
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

```javascript
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](https://github.com/nemanjan00/queue-promised/tree/master/docs). 

## Authors

* [nemanjan00](https://github.com/nemanjan00)

---
_Source: https://npm.io/package/queue-promised · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
