-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from github/polyfill-with-resolvers
polyfill for Promise.withResolvers
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
}) |