Skip to content

Commit 2fecfc7

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[cleanup] Use Promise with resolvers
Bug: none Change-Id: I854b980b875f915b9257b7a663473a3726eb9133 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6225539 Reviewed-by: Benedikt Meurer <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]>
1 parent 0b015e4 commit 2fecfc7

File tree

8 files changed

+12
-35
lines changed

8 files changed

+12
-35
lines changed

front_end/core/common/ResolverBase.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export abstract class ResolverBase<Id, T> {
3434
}
3535

3636
/**
37-
* Resolve the `id`. Returns the object immediatelly if it can be resolved,
37+
* Resolve the `id`. Returns the object immediately if it can be resolved,
3838
* and otherwise waits for the object to appear and calls `callback` once
3939
* it is resolved.
4040
*/
@@ -68,12 +68,7 @@ export abstract class ResolverBase<Id, T> {
6868
if (promiseInfo) {
6969
return promiseInfo.promise;
7070
}
71-
let resolve: (value: T) => void = () => {};
72-
let reject: (error: Error) => void = () => {};
73-
const promise = new Promise<T>((res, rej) => {
74-
resolve = res;
75-
reject = rej;
76-
});
71+
const {resolve, reject, promise} = Promise.withResolvers<T>();
7772
this.#unresolvedIds.set(id, {promise, resolve, reject});
7873
this.startListening();
7974
return promise;

front_end/models/persistence/IsolatedFileSystemManager.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ export class IsolatedFileSystemManager extends Common.ObjectWrapper.ObjectWrappe
132132
}
133133

134134
private requestFileSystems(): Promise<IsolatedFileSystem[]> {
135-
let fulfill: (arg0: IsolatedFileSystem[]) => void;
136-
const promise = new Promise<IsolatedFileSystem[]>(f => {
137-
fulfill = f;
138-
});
135+
const {resolve, promise} = Promise.withResolvers<IsolatedFileSystem[]>();
139136
Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
140137
Host.InspectorFrontendHostAPI.Events.FileSystemsLoaded, onFileSystemsLoaded, this);
141138
Host.InspectorFrontendHost.InspectorFrontendHostInstance.requestFileSystems();
@@ -153,7 +150,7 @@ export class IsolatedFileSystemManager extends Common.ObjectWrapper.ObjectWrappe
153150
}
154151

155152
function onFileSystemsAdded(fileSystems: (IsolatedFileSystem|null)[]): void {
156-
fulfill(fileSystems.filter(fs => Boolean(fs)) as IsolatedFileSystem[]);
153+
resolve(fileSystems.filter(fs => Boolean(fs)) as IsolatedFileSystem[]);
157154
}
158155
}
159156

front_end/models/workspace/UISourceCode.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ export class UISourceCode extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
160160
}
161161

162162
rename(newName: Platform.DevToolsPath.RawPathString): Promise<boolean> {
163-
let fulfill: (arg0: boolean) => void;
164-
const promise = new Promise<boolean>(x => {
165-
fulfill = x;
166-
});
163+
const {resolve, promise} = Promise.withResolvers<boolean>();
167164
this.projectInternal.rename(this, newName, innerCallback.bind(this));
168165
return promise;
169166

@@ -175,7 +172,7 @@ export class UISourceCode extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
175172
newName as Platform.DevToolsPath.RawPathString, newURL as Platform.DevToolsPath.UrlString,
176173
newContentType as Common.ResourceType.ResourceType);
177174
}
178-
fulfill(success);
175+
resolve(success);
179176
}
180177
}
181178

front_end/panels/recorder/models/RecordingPlayer.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,10 @@ export class RecordingPlayer extends Common.ObjectWrapper.ObjectWrapper<EventTyp
243243
}
244244

245245
override async beforeEachStep?(step: Step, flow: UserFlow): Promise<void> {
246-
let resolver: () => void = () => {};
247-
const promise = new Promise<void>(r => {
248-
resolver = r;
249-
});
246+
const {resolve, promise} = Promise.withResolvers<void>();
250247
player.dispatchEventToListeners(Events.STEP, {
251248
step,
252-
resolve: resolver,
249+
resolve,
253250
});
254251
await promise;
255252
const currentStepIndex = flow.steps.indexOf(step);

front_end/testing/MockConnection.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ export function clearAllMockConnectionResponseHandlers() {
5757
export function registerListenerOnOutgoingMessage(method: ProtocolCommand): Promise<void> {
5858
let outgoingMessageListenerEntry = outgoingMessageListenerEntryMap.get(method);
5959
if (!outgoingMessageListenerEntry) {
60-
let resolve = () => {};
61-
const promise = new Promise<void>(r => {
62-
resolve = r;
63-
});
60+
const {resolve, promise} = Promise.withResolvers<void>();
6461
outgoingMessageListenerEntry = {promise, resolve};
6562
outgoingMessageListenerEntryMap.set(method, outgoingMessageListenerEntry);
6663
}

front_end/testing/TrackAsyncOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function getStack(error: Error): string {
245245
return (error.stack ?? 'No stack').split('\n').slice(2).join('\n');
246246
}
247247

248-
// We can't use Sinon for stubbing as 1) we need to double wrap somtimes and 2)
248+
// We can't use Sinon for stubbing as 1) we need to double wrap sometimes and 2)
249249
// we need to access original values.
250250
interface Stub<TKey extends keyof typeof window> {
251251
name: TKey;

front_end/ui/components/issue_counter/IssueLinkIcon.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ class MockIssueResolver {
5757
if (entry) {
5858
return entry.promise;
5959
}
60-
let resolve: (issue: IssuesManager.Issue.Issue|null) => void = () => {};
61-
const promise = new Promise<IssuesManager.Issue.Issue|null>(r => {
62-
resolve = r;
63-
});
60+
const {resolve, promise} = Promise.withResolvers<IssuesManager.Issue.Issue|null>();
6461
this.#promiseMap.set(issueId, {resolve, promise});
6562
return promise;
6663
}

front_end/ui/components/request_link_icon/RequestLinkIcon.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ class MockRequestResolver {
6464
if (entry) {
6565
return entry.promise;
6666
}
67-
let resolve: (request: SDK.NetworkRequest.NetworkRequest|null) => void = () => {};
68-
const promise = new Promise<SDK.NetworkRequest.NetworkRequest|null>(r => {
69-
resolve = r;
70-
});
67+
const {resolve, promise} = Promise.withResolvers<SDK.NetworkRequest.NetworkRequest|null>();
7168
this.#promiseMap.set(requestId, {resolve, promise});
7269
return promise;
7370
}

0 commit comments

Comments
 (0)