# @st-lib/parallel

> Parallel execution of callbacks for each element of the iterable

Latest version **1.0.0** (published 2020-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @st-lib/parallel
pnpm add @st-lib/parallel
yarn add @st-lib/parallel
bun add @st-lib/parallel
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-09-28 |
| First published | 2020-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | padreramnt |
| Maintainers | st-lib |
| Keywords | st-lib, parallel |

## Links

- npm: https://www.npmjs.com/package/@st-lib/parallel
- Repository: https://github.com/Padreramnt/st-lib--parallel
- Homepage: https://github.com/Padreramnt/st-lib--parallel#readme
- Issues: https://github.com/Padreramnt/st-lib--parallel/issues
- npm.io page: https://npm.io/package/@st-lib/parallel

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.0.0
- [@st-lib/is](https://npm.io/package/@st-lib/is.md) ^1.0.5

## Recent versions

- 1.0.0 (latest) — 2020-09-28

## README

# Parallel execution of callbacks for each element of the iterable

``` tsx
import { runDependently, runIndependently, config } from '@st-lib/parallel'


function* getALotOfItems(count = 20e20) {
	for (let i =0; i < count; i++) {
		yield i
	}
}

// globally set the default number of threads
config.defaultThreads = 40

function doSomeHeavyCalculations(inp: number) {
	return new Promise<number>((res, rej) => {
		if (Math.random() < 0.0001) {
			setTimeout(rej, 100 + Math.random() * 4000, new Error(`something very bad happened to the ${inp}`))
		} else {
			setTimeout(res, 100 + Math.random() * 4000, inp * 2)
		}
	})
}

async function exampleRunDependently() {
	try {
		const results = await runDependently(getALotOfItems(), doSomeHeavyCalculations)
		console.log(results)
	} catch (error) {
		console.error(error)
		// catch the error
	}
}

async function exampleRunIndependently() {
	const { results, ok, errors, values } = await runIndependently(getALotOfItems(), doSomeHeavyCalculations)
	// variant 1: switch via `ok`
	if (ok) {
		// all ok, values contain returned values of each callback, similar to runDependently
		console.log(values)
	} else {
		// something failed, need process errors
		console.log(errors)
	}
	// variant 2: process each result separately
	for (const result of results) {
		if (result.ok) {
			console.log(result.data)
		} else {
			console.log(result.error)
		}
	}
	// variant 3: combine as u need
}
```

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