-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dea0f50
commit fd99742
Showing
4 changed files
with
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: withResolvers | ||
description: Ponyfill for Promise.withResolvers() | ||
--- | ||
|
||
### Usage | ||
|
||
Creates a new promise and returns the resolve and reject functions along with the promise itself. | ||
|
||
The ponyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers | ||
|
||
```ts | ||
const { resolve, reject, promise } = withResolvers() | ||
|
||
resolve(42) | ||
``` |
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 @@ | ||
interface PromiseWithResolvers<T> { | ||
promise: Promise<T> | ||
resolve: (value: T | PromiseLike<T>) => void | ||
reject: (reason?: any) => void | ||
} | ||
|
||
/** | ||
* Creates a new promise and returns the resolve and reject functions along with the promise itself. | ||
* | ||
* The ponyfill for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers | ||
* | ||
* @see https://radashi-org.github.io/reference/async/withResolvers | ||
* @example | ||
* ```ts | ||
* const {resolve, reject, promise} = withResolvers() | ||
* | ||
* resolve(42) | ||
* ``` | ||
*/ | ||
export function withResolvers<T>(): PromiseWithResolvers<T> { | ||
let resolve: any | ||
let reject: any | ||
|
||
const promise = new Promise<T>((res, rej) => { | ||
resolve = res | ||
reject = rej | ||
}) | ||
|
||
return { resolve, reject, promise } | ||
} |
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,19 @@ | ||
import * as _ from 'radashi' | ||
|
||
describe('withResolvers', () => { | ||
test('resolves promise', async () => { | ||
const { resolve, promise } = _.withResolvers<number>() | ||
|
||
resolve(42) | ||
|
||
expect(await promise).toBe(42) | ||
}) | ||
|
||
test('rejects promise', async () => { | ||
const { reject, promise } = _.withResolvers<number>() | ||
|
||
reject('Weird error') | ||
|
||
await expect(promise).rejects.toThrowError('Weird error') | ||
}) | ||
}) |