ps

p-settle

Settle promises concurrently and get their fulfillment value or rejection reason

Showing:

Popularity

Downloads/wk

122K

GitHub Stars

71

Maintenance

Last Commit

1mo ago

Contributors

5

Package

Dependencies

2

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

p-settle

Settle promises concurrently and get their fulfillment value or rejection reason

Install

$ npm install p-settle

Usage

import fs from 'node:fs/promises';
import pSettle from 'p-settle';

const files = [
    'a.txt',
    'b.txt' // Doesn't exist
].map(fileName => fs.readFile(fileName, 'utf8'));

console.log(await pSettle(files));
/*
[
    {
        isFulfilled: true,
        isRejected: false,
        value: '🦄'
    },
    {
        isFulfilled: false,
        isRejected: true,
        reason: [Error: ENOENT: no such file or directory, open 'b.txt']
    }
]
*/

API

pSettle(array, options?)

Returns a Promise<object[]> that is fulfilled when all promises from the array argument are settled.

The objects in the array have the following properties:

  • isFulfilled
  • isRejected
  • value or reason (Depending on whether the promise fulfilled or rejected)

array

Type: Array<ValueType | PromiseLike<ValueType> | ((...args: any[]) => PromiseLike<ValueType>)>

The array can contain a mix of any value, promise, and async function. Promises are awaited. Async functions are executed and awaited. The concurrency option only works for elements that are async functions.

options

Type: object

concurrency

Type: number (Integer)\ Default: Infinity\ Minimum: 1

The number of concurrently pending promises.

Note: This only limits concurrency for elements that are async functions, not promises.

  • p-reflect - Make a promise always fulfill with its actual fulfillment value or rejection reason
  • p-map - Map over promises concurrently
  • More…

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100