MCPcopy
hub / github.com/graphql/graphql-js / withAsyncUsing

Function withAsyncUsing

src/__testUtils__/withAsyncUsing.ts:23–56  ·  view source on GitHub ↗
(
  value: T,
  useResource: (resource: T) => void | Promise<void>,
)

Source from the content-addressed store, hash-verified

21 Symbol.asyncDispose ?? Symbol.for('Symbol.asyncDispose');
22
23export async function withAsyncUsing<T>(
24 value: T,
25 useResource: (resource: T) => void | Promise<void>,
26): Promise<void> {
27 // On Node 24+, supportsAsyncUsing is true and this branch is unreachable.
28 // On Node 22, this is the branch that gets executed.
29 /* node:coverage ignore next 26 */
30 if (!supportsAsyncUsing) {
31 try {
32 await useResource(value);
33 } finally {
34 const dispose = (value as { [key: symbol]: unknown })[asyncDispose];
35 if (typeof dispose === 'function') {
36 await dispose.call(value);
37 }
38 }
39 return;
40 }
41
42 {
43 // Keep `await using` in a dynamically compiled function so Node 22 can still
44 // parse this file while newer runtimes exercise the real syntax.
45 // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func
46 const createRunWithAsyncUsing = new Function(
47 'return async function(value, useResource) { await using resource = value; await useResource(resource); };',
48 ) as () => (
49 value: T,
50 useResource: (resource: T) => void | Promise<void>,
51 ) => Promise<void>;
52
53 const runWithAsyncUsing = createRunWithAsyncUsing();
54 await runWithAsyncUsing(value, useResource);
55 }
56}

Calls

no outgoing calls

Tested by

no test coverage detected