Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polyfill for Promise.withResolvers #67

Merged
merged 3 commits into from
Oct 17, 2024
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
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as clipboardItem from './clipboarditem.js'
import * as elementCheckVisibility from './element-checkvisibility.js'
import * as navigatorClipboard from './navigator-clipboard.js'
import * as withResolvers from './promise-withResolvers.js'
import * as requestIdleCallback from './requestidlecallback.js'

let supportsModalPseudo = false
Expand Down Expand Up @@ -47,6 +48,7 @@ export const polyfills = {
elementCheckVisibility,
navigatorClipboard,
requestIdleCallback,
withResolvers,
}

export function isSupported() {
Expand Down
30 changes: 30 additions & 0 deletions src/promise-withResolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*#__PURE__*/
export function withResolvers<T>(this: PromiseConstructor) {
const out = {} as {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reject: (reason?: any) => void
}
out.promise = new Promise<T>((resolve, reject) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - I would new this here, but we call this in tests directly, so haven't done so

out.resolve = resolve
out.reject = reject
})
return out
}

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

/*#__PURE__*/
export function isPolyfilled(): boolean {
return 'withResolvers' in Promise && Promise.withResolvers === withResolvers
}

export function apply(): void {
if (!isSupported()) {
Object.assign(Promise, {withResolvers})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using Object.assign to avoid type errors from defining withResolvers on Promise

}
}
40 changes: 40 additions & 0 deletions test/promise-withResolvers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {apply, isPolyfilled, isSupported, withResolvers} from '../lib/promise-withResolvers.js'

describe('withResolvers', () => {
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('resolves to first resolving value', async () => {
const arg = withResolvers()
expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve'])
expect(arg).to.have.property('promise').to.be.a('promise')
expect(arg).to.have.property('resolve').to.be.a('function')
expect(arg).to.have.property('reject').to.be.a('function')

arg.resolve(1)
expect(await arg.promise).to.be.eql(1)
})

it('rejects to first rejecting reason', async () => {
const arg = withResolvers()
expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve'])
expect(arg).to.have.property('promise').to.be.a('promise')
expect(arg).to.have.property('resolve').to.be.a('function')
expect(arg).to.have.property('reject').to.be.a('function')

const err = new Error('rejected')

try {
arg.reject(err)
await arg.promise
expect.fail('should fail')
} catch (e) {
expect(e).to.be.eql(err)
}
})
})