# p-progress

> Create a promise that reports progress

Latest version **1.0.0** (published 2023-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install p-progress
pnpm add p-progress
yarn add p-progress
bun add p-progress
```

## 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 | 2023-11-04 |
| First published | 2017-09-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 1 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 766 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | promise, progress, events, event, async, function, await, promises, bluebird |

## Links

- npm: https://www.npmjs.com/package/p-progress
- Repository: https://github.com/sindresorhus/p-progress
- Homepage: https://github.com/sindresorhus/p-progress#readme
- Issues: https://github.com/sindresorhus/p-progress/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/p-progress

## Dependencies (1)

- [p-times](https://npm.io/package/p-times.md) ^4.0.0

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

- 1.0.0 (latest) — 2023-11-04
- 0.6.0 — 2021-11-03
- 0.5.1 — 2021-04-09
- 0.5.0 — 2021-01-07
- 0.4.2 — 2020-02-21
- 0.4.1 — 2019-08-18
- 0.4.0 — 2019-04-01
- 0.3.0 — 2019-03-12
- 0.2.0 — 2018-07-11
- 0.1.1 — 2017-09-11
- 0.1.0 — 2017-09-11

## README

# p-progress

> Create a promise that reports progress

Useful for reporting progress to the user during long-running async operations.

## Install

```sh
npm install p-progress
```

## Usage

```js
import pProgress from 'p-progress';

const runJob = async name => pProgress(async progress => {
	const job = new Job(name);

	job.on('data', data => {
		progress(data.length / job.totalSize);
	});

	await job.run();
});

const progressPromise = runJob('Gather rainbows');

progressPromise.onProgress(console.log);
//=> 0.09
//=> 0.23
//=> 0.59
//=> 0.75
//=> 1

await progressPromise;
```

## API

### pProgress(function)

Convenience method to make your promise-returning or async function report progress.

The function you specify will be passed the `progress()` function as a parameter.

### instance = new PProgress(executor)

Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `progress` parameter in `executor`.

`PProgress` is a subclass of `Promise`.

###### progress(percentage)

Type: `Function`

Call this with progress updates. It expects a number between 0 and 1.

Multiple calls with the same number will result in only one `onProgress()`
event.

Calling with a number lower than previously will be ignored.

Progress percentage `1` is reported for you when the promise resolves. If you set it yourself, it will simply be ignored.

#### instance.progress

Type: `number`

The current progress percentage of the promise as a number between 0 and 1.

#### instance.onProgress(function)

Accepts a function that gets `instance.progress` as an argument and is called for every progress event.

```js
import {PProgress} from 'p-progress';

const progressPromise = new PProgress((resolve, reject, progress) => {
	const job = new Job();

	job.on('data', data => {
		progress(data.length / job.totalSize);
	});

	job.on('finish', resolve);
	job.on('error', reject);
});

progressPromise.onProgress(progress => {
	console.log(`${progress * 100}%`);
	//=> 9%
	//=> 23%
	//=> 59%
	//=> 75%
	//=> 100%
});

await progressPromise;
```

### PProgress.all(promises, options?)

Convenience method to run multiple promises and get a total progress of all of them. It counts normal promises with progress `0` when pending and progress `1` when resolved. For `PProgress` type promises, it listens to their `onProgress()` method for more fine grained progress reporting. You can mix and match normal promises and `PProgress` promises.

### PProgress.allSettled(promises, options?)

Like [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) but also exposes the total progress of all of the promises like `PProgress.all`.

```js
import pProgress, {PProgress} from 'p-progress';
import delay from 'delay';

const progressPromise = () => pProgress(async progress => {
	progress(0.14);
	await delay(52);
	progress(0.37);
	await delay(104);
	progress(0.41);
	await delay(26);
	progress(0.93);
	await delay(55);
	return 1;
});

const progressPromise2 = () => pProgress(async progress => {
	progress(0.14);
	await delay(52);
	progress(0.37);
	await delay(104);
	progress(0.41);
	await delay(26);
	progress(0.93);
	await delay(55);
	throw new Error('Catch me if you can!');
});

const allProgressPromise = PProgress.allSettled([
	progressPromise(),
	progressPromise2()
]);

allProgressPromise.onProgress(console.log);
//=> 0.0925
//=> 0.3425
//=> 0.5925
//=> 0.6025
//=> 0.7325
//=> 0.9825
//=> 1

console.log(await allProgressPromise);
//=> [{status: 'fulfilled', value: 1}, {status: 'rejected', reason: Error: Catch me if you can!}]
```

#### promises

Type: `Promise[]`

An array of promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).

#### options

Type: `object`

##### concurrency

Type: `number`\
Default: `Infinity`\
Minimum: `1`

The number of concurrently pending promises.

To run the promises in series, set it to `1`.

When this option is set, the first argument must be an array of promise-returning functions.

## Related

- [p-cancelable](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled
- [More…](https://github.com/sindresorhus/promise-fun)

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