Skip to content

Commit 1c39356

Browse files
committed
introduce new afterMaybeAsync helper
`after` becomes sync, requiring no return type check for the onFulfilled result.
1 parent 025f26e commit 1c39356

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

src/execution/execute.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { after } from '../jsutils/after.js';
2+
import { afterMaybeAsync } from '../jsutils/afterMaybeAsync.js';
23
import { catchAfter } from '../jsutils/catchAfter.js';
34
import { inspect } from '../jsutils/inspect.js';
45
import { invariant } from '../jsutils/invariant.js';
@@ -1282,7 +1283,7 @@ function completeAbstractValue(
12821283
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
12831284

12841285
if (isPromise(runtimeType)) {
1285-
return after(runtimeType, (resolvedRuntimeType) =>
1286+
return afterMaybeAsync(runtimeType, (resolvedRuntimeType) =>
12861287
completeObjectValue(
12871288
exeContext,
12881289
ensureValidRuntimeType(
@@ -1394,7 +1395,7 @@ function completeObjectValue(
13941395
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
13951396

13961397
if (isPromise(isTypeOf)) {
1397-
return after(isTypeOf, (resolvedIsTypeOf) => {
1398+
return afterMaybeAsync(isTypeOf, (resolvedIsTypeOf) => {
13981399
if (!resolvedIsTypeOf) {
13991400
throw invalidReturnTypeError(returnType, result, fieldNodes);
14001401
}
@@ -2314,7 +2315,7 @@ class DeferredFragmentRecord {
23142315
addData(data: PromiseOrValue<ObjMap<unknown> | null>) {
23152316
const parentData = this.parentContext?.promise;
23162317
if (parentData) {
2317-
this._resolve?.(after(parentData, () => data));
2318+
this._resolve?.(afterMaybeAsync(parentData, () => data));
23182319
return;
23192320
}
23202321
this._resolve?.(data);
@@ -2368,7 +2369,7 @@ class StreamRecord {
23682369
addItems(items: PromiseOrValue<Array<unknown> | null>) {
23692370
const parentData = this.parentContext?.promise;
23702371
if (parentData) {
2371-
this._resolve?.(after(parentData, () => items));
2372+
this._resolve?.(afterMaybeAsync(parentData, () => items));
23722373
return;
23732374
}
23742375
this._resolve?.(items);

src/jsutils/after.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { isPromise } from './isPromise.js';
2-
import type { PromiseOrValue } from './PromiseOrValue.js';
3-
41
/**
52
* Async Helper Function that avoides `.then()`
63
*
@@ -11,11 +8,8 @@ import type { PromiseOrValue } from './PromiseOrValue.js';
118
*/
129
export async function after<T, R>(
1310
promise: Promise<T>,
14-
onFulfilled: (value: T) => PromiseOrValue<R>,
11+
onFulfilled: (value: T) => R,
1512
): Promise<R> {
1613
const result = onFulfilled(await promise);
17-
if (isPromise(result)) {
18-
return await result;
19-
}
2014
return result;
2115
}

src/jsutils/afterMaybeAsync.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { isPromise } from './isPromise.js';
2+
import type { PromiseOrValue } from './PromiseOrValue.js';
3+
4+
/**
5+
* Async Helper Function that avoides `.then()`
6+
*
7+
* It is faster to await a promise prior to returning it from an async function
8+
* than to return a promise with `.then()`.
9+
*
10+
* see: https://github.com/tc39/proposal-faster-promise-adoption
11+
*/
12+
export async function afterMaybeAsync<T, R>(
13+
promise: Promise<T>,
14+
onFulfilled: (value: T) => PromiseOrValue<R>,
15+
): Promise<R> {
16+
const result = onFulfilled(await promise);
17+
if (isPromise(result)) {
18+
return await result;
19+
}
20+
return result;
21+
}

src/jsutils/promiseReduce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { after } from './after.js';
1+
import { afterMaybeAsync } from './afterMaybeAsync.js';
22
import { isPromise } from './isPromise.js';
33
import type { PromiseOrValue } from './PromiseOrValue.js';
44

@@ -17,7 +17,7 @@ export function promiseReduce<T, U>(
1717
let accumulator = initialValue;
1818
for (const value of values) {
1919
accumulator = isPromise(accumulator)
20-
? after(accumulator, (resolved) => callbackFn(resolved, value))
20+
? afterMaybeAsync(accumulator, (resolved) => callbackFn(resolved, value))
2121
: callbackFn(accumulator, value);
2222
}
2323
return accumulator;

0 commit comments

Comments
 (0)