
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Settle promises concurrently and get their fulfillment value or rejection reason with optional limited concurrency
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.
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' }
]
*/
});
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.
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.
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.
Settle promises concurrently and get their fulfillment value or rejection reason with optional limited concurrency
npm install p-settle
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));
/*
[
{
status: 'fulfilled',
value: '🦄',
isFulfilled: true,
isRejected: false,
},
{
status: 'rejected',
reason: [Error: ENOENT: no such file or directory, open 'b.txt'],
isFulfilled: false,
isRejected: true,
}
]
*/
With a mapper
function:
import fs from 'node:fs/promises';
import pSettle from 'p-settle';
const files = ['a.txt', 'b.txt']; // Filenames
console.log(await pSettle(files, {
mapper: filename => fs.readFile(filename, 'utf8'),
concurrency: 2
}));
/*
[
{
status: 'fulfilled',
value: '🦄',
isFulfilled: true,
isRejected: false,
},
{
status: 'rejected',
reason: [Error: ENOENT: no such file or directory, open 'b.txt'],
isFulfilled: false,
isRejected: true,
}
]
*/
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
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.
When using the mapper
option, array
can be of any type since the mapper
function will transform each element.
Type: object
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. When using the mapper
option, concurrency applies to the mapped functions.
Type: Function
Function which is called for every item in array
. Expected to return a promise or value.
The mapper function receives two arguments:
element
- The current element being processedindex
- The index of the element in the source arrayWhen provided, the mapper
function transforms each element in the array before settling it. This allows you to work with arrays of any type of data.
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
.
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
.
FAQs
Settle promises concurrently and get their fulfillment value or rejection reason with optional limited concurrency
We found that p-settle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.