Skip to content

Commit

Permalink
try just one helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Dec 19, 2022
1 parent 71678fd commit 682005a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 83 deletions.
33 changes: 15 additions & 18 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { after } from '../jsutils/after.js';
import { afterMaybeAsync } from '../jsutils/afterMaybeAsync.js';
import { catchAfter } from '../jsutils/catchAfter.js';
import { inspect } from '../jsutils/inspect.js';
import { invariant } from '../jsutils/invariant.js';
import { isAsyncIterable } from '../jsutils/isAsyncIterable.js';
Expand All @@ -15,7 +13,6 @@ import { addPath, pathToArray } from '../jsutils/Path.js';
import { promiseForObject } from '../jsutils/promiseForObject.js';
import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
import { promiseReduce } from '../jsutils/promiseReduce.js';
import { tryAfter } from '../jsutils/tryAfter.js';

import type { GraphQLFormattedError } from '../error/GraphQLError.js';
import { GraphQLError } from '../error/GraphQLError.js';
Expand Down Expand Up @@ -349,7 +346,7 @@ function executeImpl(
try {
const result = executeOperation(exeContext);
if (isPromise(result)) {
return tryAfter(
return after(
result,
(data) => {
const initialResult = buildResponse(data, exeContext.errors);
Expand Down Expand Up @@ -650,7 +647,7 @@ function executeFields(
}
} catch (error) {
if (containsPromise) {
return tryAfter(
return after(
promiseForObject(results),
() => {
throw error;
Expand Down Expand Up @@ -747,7 +744,7 @@ function executeField(
);

if (isPromise(completed)) {
return catchAfter(completed, (rawError) => {
return after(completed, undefined, (rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(path));
const handledError = handleFieldError(error, returnType, errors);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
Expand Down Expand Up @@ -1221,7 +1218,7 @@ function completeListItemValue(
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
completedResults.push(
catchAfter(completedItem, (rawError) => {
after(completedItem, undefined, (rawError) => {
const error = locatedError(
rawError,
fieldNodes,
Expand Down Expand Up @@ -1283,7 +1280,7 @@ function completeAbstractValue(
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);

if (isPromise(runtimeType)) {
return afterMaybeAsync(runtimeType, (resolvedRuntimeType) =>
return after(runtimeType, (resolvedRuntimeType) =>
completeObjectValue(
exeContext,
ensureValidRuntimeType(
Expand Down Expand Up @@ -1395,7 +1392,7 @@ function completeObjectValue(
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);

if (isPromise(isTypeOf)) {
return afterMaybeAsync(isTypeOf, (resolvedIsTypeOf) => {
return after(isTypeOf, (resolvedIsTypeOf) => {
if (!resolvedIsTypeOf) {
throw invalidReturnTypeError(returnType, result, fieldNodes);
}
Expand Down Expand Up @@ -1765,7 +1762,7 @@ function createSourceEventStreamImpl(
try {
const eventStream = executeSubscription(exeContext);
if (isPromise(eventStream)) {
return catchAfter(eventStream, (error) => ({ errors: [error] }));
return after(eventStream, undefined, (error) => ({ errors: [error] }));
}

return eventStream;
Expand Down Expand Up @@ -1836,7 +1833,7 @@ function executeSubscription(
const result = resolveFn(rootValue, args, contextValue, info);

if (isPromise(result)) {
return tryAfter(result, assertEventStream, (error) => {
return after(result, assertEventStream, (error) => {
throw locatedError(error, fieldNodes, pathToArray(path));
});
}
Expand Down Expand Up @@ -1890,7 +1887,7 @@ function executeDeferredFragment(
);

if (isPromise(promiseOrData)) {
promiseOrData = catchAfter(promiseOrData, (error) => {
promiseOrData = after(promiseOrData, undefined, (error) => {
asyncPayloadRecord.errors.push(error);
return null;
});
Expand Down Expand Up @@ -1929,7 +1926,7 @@ function executeStreamField(
item,
asyncPayloadRecord,
);
const completedItems = tryAfter(
const completedItems = after(
completedItem,
(resolved) => [resolved],
(error) => {
Expand Down Expand Up @@ -1972,7 +1969,7 @@ function executeStreamField(
}

if (isPromise(completedItem)) {
const completedItems = tryAfter(
const completedItems = after(
completedItem,
(resolved) => [resolved],
(rawError) => {
Expand Down Expand Up @@ -2041,7 +2038,7 @@ async function executeStreamIteratorItem(
);

if (isPromise(completedItem)) {
completedItem = catchAfter(completedItem, (rawError) => {
completedItem = after(completedItem, undefined, (rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
Expand Down Expand Up @@ -2115,7 +2112,7 @@ async function executeStreamIterator(

let completedItems: PromiseOrValue<Array<unknown> | null>;
if (isPromise(completedItem)) {
completedItems = tryAfter(
completedItems = after(
completedItem,
(resolved) => [resolved],
(error) => {
Expand Down Expand Up @@ -2315,7 +2312,7 @@ class DeferredFragmentRecord {
addData(data: PromiseOrValue<ObjMap<unknown> | null>) {
const parentData = this.parentContext?.promise;
if (parentData) {
this._resolve?.(afterMaybeAsync(parentData, () => data));
this._resolve?.(after(parentData, () => data));
return;
}
this._resolve?.(data);
Expand Down Expand Up @@ -2369,7 +2366,7 @@ class StreamRecord {
addItems(items: PromiseOrValue<Array<unknown> | null>) {
const parentData = this.parentContext?.promise;
if (parentData) {
this._resolve?.(afterMaybeAsync(parentData, () => items));
this._resolve?.(after(parentData, () => items));
return;
}
this._resolve?.(items);
Expand Down
27 changes: 22 additions & 5 deletions src/jsutils/after.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { isPromise } from './isPromise.js';
import type { PromiseOrValue } from './PromiseOrValue.js';

/**
* Async Helper Function that avoids `.then()`
*
Expand All @@ -6,10 +9,24 @@
*
* see: https://github.com/tc39/proposal-faster-promise-adoption
*/
export async function after<T, R>(
export async function after<T, R = T, U = T>(
promise: Promise<T>,
onFulfilled: (value: T) => R,
): Promise<R> {
const result = onFulfilled(await promise);
return result;
onFulfilled?: (value: T) => PromiseOrValue<R>,
onError?: (error: any) => U,
): Promise<R | U> {
try {
const result =
onFulfilled === undefined
? ((await promise) as R)
: onFulfilled(await promise);
if (isPromise(result)) {
return await result;
}
return result;
} catch (error) {
if (onError === undefined) {
throw error;
}
return onError(error);
}
}
21 changes: 0 additions & 21 deletions src/jsutils/afterMaybeAsync.ts

This file was deleted.

18 changes: 0 additions & 18 deletions src/jsutils/catchAfter.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/jsutils/promiseReduce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterMaybeAsync } from './afterMaybeAsync.js';
import { after } from './after.js';
import { isPromise } from './isPromise.js';
import type { PromiseOrValue } from './PromiseOrValue.js';

Expand All @@ -17,7 +17,7 @@ export function promiseReduce<T, U>(
let accumulator = initialValue;
for (const value of values) {
accumulator = isPromise(accumulator)
? afterMaybeAsync(accumulator, (resolved) => callbackFn(resolved, value))
? after(accumulator, (resolved) => callbackFn(resolved, value))
: callbackFn(accumulator, value);
}
return accumulator;
Expand Down
19 changes: 0 additions & 19 deletions src/jsutils/tryAfter.ts

This file was deleted.

0 comments on commit 682005a

Please sign in to comment.