What is p-settle?
The p-settle npm package is used to settle multiple promises and get their results regardless of whether they were fulfilled or rejected. It returns an array of objects with the state and value/reason of each promise.
What are p-settle's main functionalities?
Settling multiple promises
This feature allows you to settle multiple promises and get their results regardless of whether they were fulfilled or rejected. The output is an array of objects with the status and value/reason of each promise.
const pSettle = require('p-settle');
const promises = [
Promise.resolve('Success'),
Promise.reject(new Error('Failure')),
Promise.resolve('Another success')
];
pSettle(promises).then(results => {
console.log(results);
/*
Output:
[
{ status: 'fulfilled', value: 'Success' },
{ status: 'rejected', reason: Error: Failure },
{ status: 'fulfilled', value: 'Another success' }
]
*/
});
Other packages similar to p-settle
promise-settle
The promise-settle package provides similar functionality to p-settle by settling multiple promises and returning their results. It also returns an array of objects with the state and value/reason of each promise. However, p-settle is more actively maintained and has a more modern API.
settle-promise
The settle-promise package is another alternative that settles multiple promises and returns their results. It is less popular and less actively maintained compared to p-settle, but it offers similar functionality.
promise.allsettled
The promise.allsettled package is a polyfill for the Promise.allSettled method, which is now a standard part of JavaScript. It provides the same functionality as p-settle but is built into the language itself, making it a more native solution.
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'
].map(fileName => fs.readFile(fileName, 'utf8'));
console.log(await pSettle(files));
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:
status
('fulfilled'
or 'rejected'
, depending on how the promise resolved)value
or reason
(Depending on whether the promise fulfilled or rejected)isFulfilled
isRejected
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.
isFulfilled(object)
This is a type guard for TypeScript users.
This is useful since await pSettle(promiseArray)
always returns a PromiseResult[]
. This function can be used to determine whether PromiseResult
is PromiseFulfilledResult
or PromiseRejectedResult
.
isRejected(object)
This is a type guard for TypeScript users.
This is useful since await pSettle(promiseArray)
always returns a PromiseResult[]
. This function can be used to determine whether PromiseResult
is PromiseRejectedResult
or PromiseFulfilledResult
.
Related
- p-reflect - Make a promise always fulfill with its actual fulfillment value or rejection reason
- p-map - Map over promises concurrently
- More…