Skip to content

Commit 4d46261

Browse files
author
cod1k
committed
Replace Promise.withResolvers with createPromiseResolver
Introduced a new utility function `createPromiseResolver` to handle promise creation alongside external resolvers. Updated all references of `Promise.withResolvers` in the codebase and tests to use the new utility, ensuring compatibility and maintaining consistency.
1 parent 02c0238 commit 4d46261

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

packages/cloudflare/src/flush.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ExecutionContext } from '@cloudflare/workers-types';
2+
import { createPromiseResolver } from './utils/makePromiseResolver';
23

34
type FlushLock = {
45
readonly ready: Promise<void>;
@@ -33,7 +34,7 @@ export function makeFlushLock(context: ExecutionContext): FlushLock {
3334
}
3435
let pending = 0;
3536
const originalWaitUntil = context.waitUntil.bind(context) as typeof context.waitUntil;
36-
const { promise, resolve } = Promise.withResolvers();
37+
const { promise, resolve } = createPromiseResolver();
3738
const hijackedWaitUntil: typeof originalWaitUntil = promise => {
3839
pending++;
3940
return originalWaitUntil(

packages/cloudflare/src/types.d.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type PromiseWithResolvers<T, E = unknown> = {
2+
readonly promise: Promise<T>;
3+
readonly resolve: (value?: T | PromiseLike<T>) => void;
4+
readonly reject: (reason?: E) => void;
5+
};
6+
/**
7+
* Creates an object containing a promise, along with its corresponding resolve and reject functions.
8+
*
9+
* This method provides a convenient way to create a promise and access its resolvers externally.
10+
*
11+
* @template T - The type of the resolved value of the promise.
12+
* @template E - The type of the rejected value of the promise. Defaults to `unknown`.
13+
* @return {PromiseWithResolvers<T, E>} An object containing the promise and its resolve and reject functions.
14+
*/
15+
export function createPromiseResolver<T, E = unknown>(): PromiseWithResolvers<T, E> {
16+
if ('withResolvers' in Promise && typeof Promise.withResolvers === 'function') {
17+
return Promise.withResolvers();
18+
}
19+
let resolve;
20+
let reject;
21+
const promise = new Promise<T>((res, rej) => {
22+
resolve = res;
23+
reject = rej;
24+
});
25+
return { promise, resolve, reject } as unknown as PromiseWithResolvers<T, E>;
26+
}

packages/cloudflare/test/durableobject.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as SentryCore from '@sentry/core';
33
import { afterEach, describe, expect, it, vi } from 'vitest';
44
import { instrumentDurableObjectWithSentry } from '../src';
55
import { isInstrumented } from '../src/instrument';
6+
import { createPromiseResolver } from '../src/utils/makePromiseResolver';
67

78
describe('instrumentDurableObjectWithSentry', () => {
89
afterEach(() => {
@@ -124,7 +125,7 @@ describe('instrumentDurableObjectWithSentry', () => {
124125
it('flush performs after all waitUntil promises are finished', async () => {
125126
const flush = vi.spyOn(SentryCore.Client.prototype, 'flush');
126127
const waitUntil = vi.fn();
127-
const { promise, resolve } = Promise.withResolvers();
128+
const { promise, resolve } = createPromiseResolver();
128129
process.nextTick(resolve);
129130
const testClass = vi.fn(context => ({
130131
fetch: () => {

packages/cloudflare/test/flush.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type ExecutionContext } from '@cloudflare/workers-types';
22
import { describe, expect, it, vi } from 'vitest';
33
import { makeFlushLock } from '../src/flush';
4+
import { createPromiseResolver } from '../src/utils/makePromiseResolver';
45

56
describe('Flush buffer test', () => {
67
const mockExecutionContext: ExecutionContext = {
@@ -18,7 +19,7 @@ describe('Flush buffer test', () => {
1819
);
1920
});
2021
it('should flush buffer only after all waitUntil were finished', async () => {
21-
const { promise, resolve } = Promise.withResolvers();
22+
const { promise, resolve } = createPromiseResolver();
2223
const lock = makeFlushLock(mockExecutionContext);
2324
mockExecutionContext.waitUntil(promise);
2425
process.nextTick(resolve);

packages/cloudflare/test/handler.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
1414
import { CloudflareClient } from '../src/client';
1515
import { withSentry } from '../src/handler';
1616
import { markAsInstrumented } from '../src/instrument';
17+
import { createPromiseResolver } from '../src/utils/makePromiseResolver';
1718

1819
// Custom type for hono-like apps (cloudflare handlers) that include errorHandler and onError
1920
type HonoLikeApp<Env = unknown, QueueHandlerMessage = unknown, CfHostMetadata = unknown> = ExportedHandler<
@@ -38,7 +39,7 @@ function makeWaitUntilAndTask<
3839
},
3940
>(): T {
4041
const waitUntil = vi.fn();
41-
const { promise, resolve } = Promise.withResolvers();
42+
const { promise, resolve } = createPromiseResolver();
4243
const resolver = vi.fn().mockImplementation(resolve).mockName('waitUntil.ready');
4344
Object.defineProperties(waitUntil, {
4445
ready: {

packages/cloudflare/test/request.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { setAsyncLocalStorageAsyncContextStrategy } from '../src/async';
99
import type { CloudflareOptions } from '../src/client';
1010
import { CloudflareClient } from '../src/client';
1111
import { wrapRequestHandler } from '../src/request';
12+
import { createPromiseResolver } from '../src/utils/makePromiseResolver';
1213

1314
const MOCK_OPTIONS: CloudflareOptions = {
1415
dsn: 'https://[email protected]/1337',
@@ -72,7 +73,7 @@ describe('withSentry', () => {
7273
waitUntil,
7374
} as unknown as ExecutionContext;
7475

75-
const { promise, resolve } = Promise.withResolvers();
76+
const { promise, resolve } = createPromiseResolver();
7677
const resolved = vi.fn(() => resolve());
7778
process.nextTick(resolved);
7879

packages/cloudflare/test/types.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)