-
-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): new helpers for promises
- Loading branch information
Showing
11 changed files
with
155 additions
and
44 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,5 @@ | ||
--- | ||
'@graphql-tools/utils': minor | ||
--- | ||
|
||
new `fakePromise`, `mapMaybePromise` and `fakeRejectPromise` helper functions |
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
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,18 @@ | ||
export interface PromiseWithResolvers<T> { | ||
promise: Promise<T>; | ||
resolve: (value: T | PromiseLike<T>) => void; | ||
reject: (reason: any) => void; | ||
} | ||
|
||
export function createDeferred<T>(): PromiseWithResolvers<T> { | ||
if (Promise.withResolvers) { | ||
return Promise.withResolvers<T>(); | ||
} | ||
let resolve!: (value: T | PromiseLike<T>) => void; | ||
let reject!: (reason: any) => void; | ||
const promise = new Promise<T>((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
return { promise, resolve, reject }; | ||
} |
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,61 @@ | ||
function isPromise<T>(val: T | Promise<T>): val is Promise<T> { | ||
return (val as any)?.then != null; | ||
} | ||
|
||
export function fakeRejectPromise(error: unknown): Promise<never> { | ||
if (isPromise(error)) { | ||
return error as Promise<never>; | ||
} | ||
return { | ||
then() { | ||
return this; | ||
}, | ||
catch(reject: (error: unknown) => any) { | ||
if (reject) { | ||
return fakePromise(reject(error)); | ||
} | ||
return this; | ||
}, | ||
finally(cb) { | ||
if (cb) { | ||
cb(); | ||
} | ||
return this; | ||
}, | ||
[Symbol.toStringTag]: 'Promise', | ||
}; | ||
} | ||
|
||
export function fakePromise<T>(value: T): Promise<T> { | ||
if (isPromise(value)) { | ||
return value; | ||
} | ||
// Write a fake promise to avoid the promise constructor | ||
// being called with `new Promise` in the browser. | ||
return { | ||
then(resolve: (value: T) => any) { | ||
if (resolve) { | ||
const callbackResult = resolve(value); | ||
if (isPromise(callbackResult)) { | ||
return callbackResult; | ||
} | ||
return fakePromise(callbackResult); | ||
} | ||
return this; | ||
}, | ||
catch() { | ||
return this; | ||
}, | ||
finally(cb) { | ||
if (cb) { | ||
const callbackResult = cb(); | ||
if (isPromise(callbackResult)) { | ||
return callbackResult.then(() => value); | ||
} | ||
return fakePromise(value); | ||
} | ||
return this; | ||
}, | ||
[Symbol.toStringTag]: '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
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,27 @@ | ||
import { isPromise } from '../src/jsutils.js'; | ||
import { MaybePromise } from './executor.js'; | ||
|
||
export function mapMaybePromise<T, R>( | ||
value: MaybePromise<T>, | ||
mapper: (v: T) => MaybePromise<R>, | ||
errorMapper?: (e: any) => MaybePromise<R>, | ||
): MaybePromise<R> { | ||
if (isPromise(value)) { | ||
if (errorMapper) { | ||
try { | ||
return value.then(mapper, errorMapper); | ||
} catch (e) { | ||
return errorMapper(e); | ||
} | ||
} | ||
return value.then(mapper); | ||
} | ||
if (errorMapper) { | ||
try { | ||
return mapper(value); | ||
} catch (e) { | ||
return errorMapper(e); | ||
} | ||
} | ||
return mapper(value); | ||
} |
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