# @tunnckocore/p-all

> Map or loop through promises, promise-returning or async functions, serially or in parallel, based on Promise.all! Has a hooks system: start, beforeEach, afterEach, finish.

Latest version **0.2.0** (published 2022-07-02) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @tunnckocore/p-all
pnpm add @tunnckocore/p-all
yarn add @tunnckocore/p-all
bun add @tunnckocore/p-all
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-07-02 |
| First published | 2022-06-25 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=16 <17 \|\| >=18 <19 \|\| >=20 |
| Dependencies | 0 |
| Unpacked size | 17.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 486 |
| Author | Charlike Mike Reagent |
| Maintainers | tunnckocore |
| Keywords | promises, loop, map, parallel, promise-map, p-map, p-map-series, serially, series, promise map, async functions, promise-returning, utility, foreach, for of, forawait, for loop, for-await, for await of |

## Links

- npm: https://www.npmjs.com/package/@tunnckocore/p-all
- Repository: https://github.com/tunnckoCore/opensource
- Homepage: https://github.com/tunnckoCore/opensource/tree/master/@tunnckocore/p-all
- Issues: https://github.com/tunnckoCore/opensource/issues
- Funding: https://ko-fi.com/tunnckoCore/commissions
- npm.io page: https://npm.io/package/@tunnckocore/p-all

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2022-07-02
- 0.1.3 — 2022-06-25
- 0.1.2 — 2022-06-25
- 0.1.1 — 2022-06-25
- 0.1.0 — 2022-06-25

## README

# @tunnckocore/p-all

> Map or loop through promises, promise-returning or async functions, serially
> or in parallel, based on Promise.all! Has a hooks system: start, beforeEach,
> afterEach, finish.

For usage, check the tests for now. Coverage is 100%, as always in the past 10
years.

It's similar to `Promise.all`, or `Promise.allSettled` more specifically, but
enhanced with a hooks system and ability to pass custom mapping function. It
works for promises and async functions.

```js
import { strict as assert } from 'node:assert';
import test from 'asia';
import { each, parallel, serial } from '@tunnckocore/p-all';

const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));

test('parallel - correct results and order', async () => {
	const order = [];
	const results = await parallel([
		() => delay(3000).then((x) => order.push('a')), // 8
		() => delay(300).then((x) => order.push('b')), // 2
		() => delay(1000).then((x) => order.push('c')), // 5
		() => delay(100).then((x) => order.push('d')), // 1
		() =>
			delay(2000).then((x) => {
				order.push('e');
				throw new Error('the "e" error');
			}), // 6
		() => delay(1500).then((x) => order.push('f')), // 7
		() => delay(560).then((x) => order.push('g')), // 3
		() => delay(880).then((x) => order.push('h')), // 4
	]);

	assert.deepEqual(results, [
		{ status: 'fulfilled', value: 8, index: 0 },
		{ status: 'fulfilled', value: 2, index: 1 },
		{ status: 'fulfilled', value: 5, index: 2 },
		{ status: 'fulfilled', value: 1, index: 3 },
		{
			status: 'rejected',
			reason: results[4].reason, // well.. lol
			index: 4,
		},
		{ status: 'fulfilled', value: 6, index: 5 },
		{ status: 'fulfilled', value: 3, index: 6 },
		{ status: 'fulfilled', value: 4, index: 7 },
	]);
	assert.deepEqual(order, ['d', 'b', 'g', 'h', 'c', 'f', 'e', 'a']);
});

test('serial - force run in series', async () => {
	const fixture = [
		() => delay(3000).then((x) => 'a'), // 8
		() => delay(300).then((x) => 'b'), // 2
		() => delay(1000).then((x) => 'c'), // 5
		() => delay(100).then((x) => 'd'), // 1
		() =>
			delay(2000).then((x) => {
				throw new Error('the "e" error');
			}), // 6
		() => delay(1500).then((x) => 'f'), // 7
		() => delay(560).then((x) => 'g'), // 3
		() => delay(880).then((x) => 'h'), // 4
	];

	const results = await each(fixture, { serial: true });

	assert.deepEqual(
		results.map((x) => x.value),
		['a', 'b', 'c', 'd', undefined, 'f', 'g', 'h'],
	);

	const serialResults = await serial(fixture);
	assert.deepEqual(
		serialResults.map((x) => x.value),
		['a', 'b', 'c', 'd', undefined, 'f', 'g', 'h'],
	);
});

test('custom mapper() function', async () => {
	const res = [];
	const results = await serial(
		[() => 3, 2, () => 5, () => 1, () => 4],
		function mapper(item, index) {
			res.push(item);
			return index > 2 ? item : undefined;
		},
	);

	assert.deepEqual(results, res);
});
```

## API

### each(iterable, mapper, options)

If `mapper` is object, it's assumed `options`.

Passing `options.serial` is the same as `serial`.

You can also pass `options.args` - custom arguments passed to each function.

A hooks system:

- options.start
- options.beforeEach
- options.afterEach
- options.finish

### parallel(iterable, mapper, options)

If `mapper` is object, it's assumed `options`.

### serial(iterable, mapper, options)

If `mapper` is object, it's assumed `options`.

## License

Apache-2.0, 2022

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