Create a promise that reports progress
Create a promise that reports progress
Useful for reporting progress to the user during long-running async operations.
npm install p-progressimport 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;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.
Same as the Promise constructor, but with an appended progress parameter in executor.
PProgress is a subclass of Promise.
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.
Type: number
The current progress percentage of the promise as a number between 0 and 1.
Accepts a function that gets instance.progress as an argument and is called for every progress event.
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;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.
Like Promise.allSettled but also exposes the total progress of all of the promises like PProgress.all.
…Type: Promise[]
An array of promises or promise-returning functions, similar to p-all.
Type: object
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.
No open issues yet, or sync has not completed.