-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(promise-utils): add makeDeferred function
- Loading branch information
1 parent
ed2673f
commit a91f11a
Showing
3 changed files
with
54 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
23 changes: 23 additions & 0 deletions
23
packages/utilities/src/lib/promise/make-deferred.function.spec.ts
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,23 @@ | ||
import { describe, expect, test } from '@jest/globals' | ||
import { makeDeferred } from './make-deferred.function.js' | ||
|
||
describe('makeDeferred', () => { | ||
|
||
test('returns an object containing a promise', () => { | ||
const { promise } = makeDeferred() | ||
expect(promise).toBeInstanceOf(Promise) | ||
}) | ||
|
||
test('resolves the returned promise with the returned resolveFn', async () => { | ||
const { promise, resolve } = makeDeferred() | ||
resolve('foo') | ||
await expect(promise).resolves.toBe('foo') | ||
}) | ||
|
||
test('rejects the returned promise with the returned rejectFn', async () => { | ||
const { promise, reject } = makeDeferred() | ||
reject(new Error('Foo Bar')) | ||
await expect(promise).rejects.toThrow('Foo Bar') | ||
}) | ||
|
||
}) |
30 changes: 30 additions & 0 deletions
30
packages/utilities/src/lib/promise/make-deferred.function.ts
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 @@ | ||
export interface Deferred<T> { | ||
resolve: (value: T | PromiseLike<T>) => void | ||
reject: (reason: unknown) => void | ||
promise: Promise<T> | ||
} | ||
|
||
/** | ||
* returns a `deferred` object which contains a promise and its resolve/reject functions | ||
* @example ```ts | ||
* const deferred = new makeDeferred<string>() | ||
* | ||
* // use promise | ||
* deferred.promise.then(console.log, console.error) | ||
* | ||
* // resolve | ||
* deferred.resolve('a value') | ||
* | ||
* // reject | ||
* deferred.reject(new Error('oh no!')) | ||
* ``` | ||
*/ | ||
export function makeDeferred<T>(): Deferred<T> { | ||
const deferred: Deferred<T> = <any>{} | ||
|
||
deferred.promise = new Promise<T>((res, rej) => { | ||
deferred.resolve = res | ||
deferred.reject = rej | ||
}) | ||
return deferred | ||
} |