Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as cryptoRandomUUID from './crypto-randomuuid.js'
import * as elementReplaceChildren from './element-replacechildren.js'
import * as eventAbortSignal from './event-abortsignal.js'
import * as objectHasOwn from './object-hasown.js'
import * as promiseAllSettled from './promise-allsettled.js'
import * as promiseAny from './promise-any.js'
import * as requestIdleCallback from './requestidlecallback.js'

Expand All @@ -32,7 +33,7 @@ const baseSupport =
'flatMap' in Array.prototype &&
'trimEnd' in String.prototype &&
// ES2020
'allSettled' in Promise &&
//'allSettled' in Promise && // Polyfilled
Comment thread
keithamus marked this conversation as resolved.
'matchAll' in String.prototype &&
// ES2021
'replaceAll' in String.prototype &&
Expand All @@ -51,6 +52,7 @@ const polyfills = [
elementReplaceChildren,
eventAbortSignal,
objectHasOwn,
promiseAllSettled,
promiseAny,
requestIdleCallback
]
Expand Down
29 changes: 29 additions & 0 deletions src/promise-allsettled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function promiseAllSettled<T extends readonly unknown[] | []>(
values: T
): Promise<{-readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>}> {
return Promise.all(
values.map(p =>
// eslint-disable-next-line github/no-then
Promise.resolve(p).then(
(value: unknown) => ({status: 'fulfilled', value}),
(reason: unknown) => ({status: 'rejected', reason})
)
)
) as unknown as Promise<{-readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>}>
}

/*#__PURE__*/
export function isSupported(): boolean {
return 'allSettled' in Promise && typeof Promise.allSettled === 'function'
}

/*#__PURE__*/
export function isPolyfilled(): boolean {
return Promise.all === promiseAllSettled
}

export function apply(): void {
if (!isSupported()) {
Promise.allSettled = promiseAllSettled
}
}
23 changes: 23 additions & 0 deletions test/promise-allsettled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {apply, isPolyfilled, isSupported, promiseAllSettled} from '../lib/promise-allsettled.js'

describe('Promise.allSettled', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})

it('returns list of Promise value/rejections', async () => {
expect(
// eslint-disable-next-line prefer-promise-reject-errors
await promiseAllSettled([Promise.resolve(1), Promise.reject(2), Promise.resolve(3), Promise.reject(4)])
).to.eql([
{status: 'fulfilled', value: 1},
{status: 'rejected', reason: 2},
{status: 'fulfilled', value: 3},
{status: 'rejected', reason: 4}
])
})
})