Skip to content

Commit af33647

Browse files
JeanMechethePunderWoman
authored andcommitted
refactor(core): remove usages of Promise.withResolvers (#63856)
Promise.withResolvers is Baseline 2024. Our policy is to support browsers in the scope of Basline widely available. Fixes #63855 PR Close #63856
1 parent e523384 commit af33647

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

packages/core/src/defer/triggering.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import {
7171
getTDeferBlockDetails,
7272
} from './utils';
7373
import {ApplicationRef} from '../application/application_ref';
74-
import type {PromiseConstructor} from '../util/promise_with_resolvers';
74+
import {promiseWithResolvers} from '../util/promise_with_resolvers';
7575

7676
/**
7777
* Schedules triggering of a defer block for `on idle` and `on timer` conditions.
@@ -550,7 +550,7 @@ function cleanupRemainingHydrationQueue(
550550
*/
551551
function populateHydratingStateForQueue(registry: DehydratedBlockRegistry, queue: string[]) {
552552
for (let blockId of queue) {
553-
registry.hydrating.set(blockId, (Promise as unknown as PromiseConstructor).withResolvers());
553+
registry.hydrating.set(blockId, promiseWithResolvers<void>());
554554
}
555555
}
556556

packages/core/src/render3/instructions/animation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {Renderer} from '../interfaces/renderer';
1919
import {NgZone} from '../../zone';
2020
import {determineLongestAnimation, allLeavingAnimations} from '../../animation/longest_animation';
2121
import {TNode} from '../interfaces/node';
22-
import type {PromiseConstructor} from '../../util/promise_with_resolvers';
22+
import {promiseWithResolvers} from '../../util/promise_with_resolvers';
2323

2424
import {
2525
areAnimationsDisabled,
@@ -230,7 +230,7 @@ function runLeaveAnimations(
230230
value: string | Function,
231231
animationsDisabled: boolean,
232232
): Promise<void> {
233-
const {promise, resolve} = (Promise as unknown as PromiseConstructor).withResolvers<void>();
233+
const {promise, resolve} = promiseWithResolvers<void>();
234234
const nativeElement = getNativeByTNode(tNode, lView) as Element;
235235

236236
ngDevMode && assertElementNodes(nativeElement, 'animate.leave');
@@ -363,7 +363,7 @@ function runLeaveAnimationFunction(
363363
tNode: TNode,
364364
value: AnimationFunction,
365365
): Promise<void> {
366-
const {promise, resolve} = (Promise as unknown as PromiseConstructor).withResolvers<void>();
366+
const {promise, resolve} = promiseWithResolvers<void>();
367367
const nativeElement = getNativeByTNode(tNode, lView) as Element;
368368

369369
ngDevMode && assertElementNodes(nativeElement, 'animate.leave');

packages/core/src/util/promise_with_resolvers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ export interface PromiseConstructor {
2727
*/
2828
withResolvers<T>(): PromiseWithResolvers<T>;
2929
}
30+
31+
/**
32+
* Replace with `Promise.withResolvers()` once it's available.
33+
* NET September 2026
34+
*
35+
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers.
36+
*/
37+
export function promiseWithResolvers<T>(): {
38+
promise: Promise<T>;
39+
resolve: (value: T | PromiseLike<T>) => void;
40+
reject: (reason?: any) => void;
41+
} {
42+
let resolve!: (value: T | PromiseLike<T>) => void;
43+
let reject!: (reason?: any) => void;
44+
45+
const promise = new Promise<T>((res, rej) => {
46+
resolve = res;
47+
reject = rej;
48+
});
49+
50+
return {promise, resolve, reject};
51+
}

packages/core/test/resource/resource_spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {
1818
signal,
1919
} from '../../src/core';
2020
import {TestBed} from '../../testing';
21-
22-
import type {PromiseConstructor} from '../../src/util/promise_with_resolvers';
21+
import {promiseWithResolvers} from '../../src/util/promise_with_resolvers';
2322

2423
abstract class MockBackend<T, R> {
2524
protected pending = new Map<
@@ -337,7 +336,7 @@ describe('resource', () => {
337336
const res = resource({
338337
params: request,
339338
loader: async ({params}) => {
340-
const p = (Promise as unknown as PromiseConstructor).withResolvers<number>();
339+
const p = promiseWithResolvers<number>();
341340
resolve.push(() => p.resolve(params));
342341
return p.promise;
343342
},

0 commit comments

Comments
 (0)