Skip to content

Commit ab7a92c

Browse files
committed
Cleaner promise pattern
1 parent 26e7b86 commit ab7a92c

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/SwiftPackage.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { BuildFlags } from "./toolchain/BuildFlags";
2323
import { SwiftToolchain } from "./toolchain/toolchain";
2424
import { isPathInsidePath } from "./utilities/filesystem";
2525
import { lineBreakRegex } from "./utilities/tasks";
26-
import { execSwift, getErrorDescription, hashString } from "./utilities/utilities";
26+
import { execSwift, getErrorDescription, hashString, unwrapPromise } from "./utilities/utilities";
2727

2828
/** Swift Package Manager contents */
2929
export interface PackageContents {
@@ -199,7 +199,6 @@ export class SwiftPackage {
199199
private _contents: SwiftPackageState | undefined;
200200
private contentsPromise: Promise<SwiftPackageState>;
201201
private contentsResolve: (value: SwiftPackageState | PromiseLike<SwiftPackageState>) => void;
202-
private contentsReject: (reason?: unknown) => void;
203202

204203
/**
205204
* SwiftPackage Constructor
@@ -213,14 +212,9 @@ export class SwiftPackage {
213212
// TODO: Make private again
214213
public workspaceState: WorkspaceState | undefined
215214
) {
216-
let res: (value: SwiftPackageState | PromiseLike<SwiftPackageState>) => void;
217-
let rej: (reason?: unknown) => void;
218-
this.contentsPromise = new Promise((resolve, reject) => {
219-
res = resolve;
220-
rej = reject;
221-
});
222-
this.contentsResolve = res!;
223-
this.contentsReject = rej!;
215+
const { promise, resolve } = unwrapPromise<SwiftPackageState>();
216+
this.contentsPromise = promise;
217+
this.contentsResolve = resolve;
224218
}
225219

226220
/**
@@ -381,14 +375,10 @@ export class SwiftPackage {
381375

382376
/** Reload swift package */
383377
public async reload(folderContext: FolderContext, disableSwiftPMIntegration: boolean = false) {
384-
let res: (value: SwiftPackageState | PromiseLike<SwiftPackageState>) => void;
385-
let rej: (reason?: unknown) => void;
386-
this.contentsPromise = new Promise((resolve, reject) => {
387-
res = resolve;
388-
rej = reject;
389-
});
390-
this.contentsResolve = res!;
391-
this.contentsReject = rej!;
378+
const { promise, resolve } = unwrapPromise<SwiftPackageState>();
379+
this.contentsPromise = promise;
380+
this.contentsResolve = resolve;
381+
392382
const loadedContents = await this.loadPackageState(
393383
folderContext,
394384
disableSwiftPMIntegration

src/utilities/utilities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,21 @@ export function compactMap<T, U>(
315315
return acc;
316316
}, []);
317317
}
318+
319+
/**
320+
* Create a promise that can be resolved outside the promise executor.
321+
* @returns An object containing the promise that can be awaited, and its resolve and reject functions.
322+
*/
323+
export function unwrapPromise<T>() {
324+
let resolve: (value: T | PromiseLike<T>) => void;
325+
let reject: (reason?: unknown) => void;
326+
const promise = new Promise<T>((res, rej) => {
327+
resolve = res;
328+
reject = rej;
329+
});
330+
return { promise, resolve: resolve!, reject: reject! };
331+
}
332+
318333
/**
319334
* Get path to swift executable, or executable in swift bin folder
320335
*

0 commit comments

Comments
 (0)