# thread-runner

> limit the number of async tasks to a set limit

Latest version **0.3.1** (published 2019-01-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install thread-runner
pnpm add thread-runner
yarn add thread-runner
bun add thread-runner
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2019-01-05 |
| First published | 2014-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | redshark1802 |
| Maintainers | redshark1802 |

## Links

- npm: https://www.npmjs.com/package/thread-runner
- Repository: https://github.com/redshark1802/thread-runner
- Homepage: https://github.com/redshark1802/thread-runner#readme
- Issues: https://github.com/redshark1802/thread-runner/issues
- npm.io page: https://npm.io/package/thread-runner

## Recent versions

- 0.3.1 (latest) — 2019-01-05
- 0.3.0 — 2015-10-17
- 0.2.0 — 2015-10-17
- 0.1.1 — 2014-08-23

## README

# Thread-runner [![Build Status](https://travis-ci.org/redshark1802/thread-runner.svg)](https://travis-ci.org/redshark1802/thread-runner)

Limit the number of asynchronous threads. Useful for doing a large amount of HTTP requests and other heavy tasks. 

```javascript

var Thread = require('thread-runner'),
    //workStack has to be an array of elements that need to be processed
    workStack = ['6', '5', '4', '3', '2' , '1', '0'];


/**
 * @param stackElement single element from workStack
 * @param callback
 */
var threadedFunction = function (stackElement, callback, stack) {

    var timeout = stackElement * 1000;
    console.log(timeout);

    setTimeout(function () {
        
        /**
         * you even can add new elements to the stack
         * stack.push('1');
         *
         */
    
        return callback(timeout);
    }, timeout);
};

//will be called once every element in workStack has been processed by threadedFunction.
var workStackDone = function(results){
    console.log('all results', results);
};

var thread = new Thread(threadedFunction, workStack, workStackDone);

//set the number of concurrent threads, default 10
thread.setMaxThreads(2);

//will be called every time threadedFunction is done processing a stackElement
var singleResult = function(result){
    console.log('single result', result);
};

thread.setListener(singleResult);

//start the runner
thread.run();

```

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