From 7dea80e1ffefa0f595822379f1fceff8ad560b22 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Tue, 29 Oct 2024 14:32:17 +0200 Subject: [PATCH] Revert "remove promiseWithResolvers" This reverts commit 93426265868cb779949225776ba39125e34b2977. it's faster actually with the helper function --- src/execution/execute.ts | 97 +++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/src/execution/execute.ts b/src/execution/execute.ts index 43cf199a40..e1b1a68bc9 100644 --- a/src/execution/execute.ts +++ b/src/execution/execute.ts @@ -13,6 +13,7 @@ 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 { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js'; import { GraphQLError } from '../error/GraphQLError.js'; import { locatedError } from '../error/locatedError.js'; @@ -867,41 +868,42 @@ function executeField( const result = resolveFn(source, args, contextValue, info, abortSignal); if (isPromise(result)) { - return new Promise((resolve, reject) => { - abortSignal?.addEventListener( - 'abort', - () => { - try { - resolve({ - rawResult: null, - incrementalDataRecords: undefined, - errors: [ - buildFieldError( - abortSignal.reason, - returnType, - fieldDetailsList, - path, - ), - ], - }); - } catch (error) { - reject(error as GraphQLError); - } - }, - { once: true }, - ); - completePromisedValue( - exeContext, - returnType, - fieldDetailsList, - info, - path, - result, - incrementalContext, - deferMap, - // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable - ).then(resolve, reject); - }); + const { promise, resolve, reject } = + promiseWithResolvers>(); + abortSignal?.addEventListener( + 'abort', + () => { + try { + resolve({ + rawResult: null, + incrementalDataRecords: undefined, + errors: [ + buildFieldError( + abortSignal.reason, + returnType, + fieldDetailsList, + path, + ), + ], + }); + } catch (error) { + reject(error); + } + }, + { once: true }, + ); + completePromisedValue( + exeContext, + returnType, + fieldDetailsList, + info, + path, + result, + incrementalContext, + deferMap, + // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable + ).then(resolve, reject); + return promise; } const completed = completeValue( @@ -2204,18 +2206,19 @@ function executeSubscription( const result = resolveFn(rootValue, args, contextValue, info, abortSignal); if (isPromise(result)) { - return new Promise((resolve, reject) => { - abortSignal?.addEventListener( - 'abort', - () => - reject( - locatedError(abortSignal.reason, fieldNodes, pathToArray(path)), - ), - { once: true }, - ); - // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable - result.then(resolve, reject); - }) + const { promise, resolve, reject } = promiseWithResolvers(); + abortSignal?.addEventListener( + 'abort', + () => + reject( + locatedError(abortSignal.reason, fieldNodes, pathToArray(path)), + ), + { once: true }, + ); + // eslint-disable-next-line @typescript-eslint/use-unknown-in-catch-callback-variable + result.then(resolve, reject); + + return promise .then(assertEventStream) .then(undefined, (error: unknown) => { throw locatedError(error, fieldNodes, pathToArray(path));