Skip to content

Commit 397fed3

Browse files
committed
Adopt Promise.withResolvers(…).
1 parent 2cbe0d3 commit 397fed3

File tree

5 files changed

+34
-36
lines changed

5 files changed

+34
-36
lines changed

script/lib/execPromise.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ export function killAllChildProcesses() {
2222
* @returns {Promise<string>}
2323
*/
2424
export function execPromise(cmd) {
25-
return new Promise((resolve, reject) => {
26-
const childProcess = exec(cmd, {}, (error, stdout, stderr) => {
27-
if (error) {
28-
reject(error);
29-
}
30-
// console.log(stdout);
31-
resolve(stdout ? stdout : stderr);
32-
});
33-
childProcesses.push(childProcess);
25+
const { promise, resolve, reject } = Promise.withResolvers();
26+
const childProcess = exec(cmd, {}, (error, stdout, stderr) => {
27+
if (error) {
28+
reject(error);
29+
}
30+
// console.log(stdout);
31+
resolve(stdout ? stdout : stderr);
3432
});
33+
childProcesses.push(childProcess);
34+
return promise;
3535
}
3636

3737
/**
@@ -49,23 +49,23 @@ export async function execPromiseLogged(cmd) {
4949
* @returns {Promise<void>}
5050
*/
5151
export function spawnPromise(cmd, args) {
52-
return new Promise((resolve, reject) => {
53-
const childProcess = spawn(cmd, args, {
54-
stdio: "inherit",
55-
// stderr: "inherit", // TODO
56-
}); // Output to shell.
57-
childProcess.on("error", (error) => {
58-
console.error(error);
59-
reject();
60-
});
61-
childProcess.on("close", (exitCode) => {
62-
exitCode === 0 ? resolve() : reject();
63-
});
64-
childProcess.on("exit", (exitCode) => {
65-
exitCode === 0 ? resolve() : reject();
66-
});
67-
childProcesses.push(childProcess);
52+
const { promise, resolve, reject } = Promise.withResolvers();
53+
const childProcess = spawn(cmd, args, {
54+
stdio: "inherit",
55+
// stderr: "inherit", // TODO
56+
}); // Output to shell.
57+
childProcess.on("error", (error) => {
58+
console.error(error);
59+
reject();
60+
});
61+
childProcess.on("close", (exitCode) => {
62+
exitCode === 0 ? resolve() : reject();
63+
});
64+
childProcess.on("exit", (exitCode) => {
65+
exitCode === 0 ? resolve() : reject();
6866
});
67+
childProcesses.push(childProcess);
68+
return promise;
6969
}
7070

7171
/**

src/cubing/twisty/views/3D/Twisty3DSceneWrapper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class Twisty3DSceneWrapper
166166
scene.remove(await old.twisty3DPuzzle());
167167
}
168168
}
169-
this.#initialWrapperTracker.handleNewValue(twisty3DPuzzleWrapper);
169+
this.#initialWrapperTracker.resolve(twisty3DPuzzleWrapper);
170170
}
171171

172172
#initialWrapperTracker = new InitialValueTracker<Twisty3DPuzzleWrapper>();
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
export class InitialValueTracker<T> {
2-
#resolve: (t: T) => void;
3-
reject: (e?: Error) => void; // TODO: AbortController?
4-
promise = new Promise<T>((resolve, reject) => {
5-
this.#resolve = resolve;
6-
this.reject = reject;
7-
});
8-
handleNewValue(t: T): void {
9-
this.#resolve(t);
2+
resolve: (t: T) => void;
3+
reject: (e?: Error) => void;
4+
promise: Promise<T>;
5+
constructor() {
6+
Object.assign(this, Promise.withResolvers());
107
}
118
}

src/cubing/twisty/views/TwistyPlayer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class TwistyPlayer
361361
case "PG3D": {
362362
// TODO: Properly wire this up so we can set PG3D for the cube.
363363
newWrapper = new Twisty3DSceneWrapper(this.experimentalModel);
364-
this.#initial3DVisualizationWrapper.handleNewValue(newWrapper);
364+
this.#initial3DVisualizationWrapper.resolve(newWrapper);
365365
break;
366366
}
367367
default:

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
3-
"target": "es2022",
3+
"target": "ES2022",
44
"module": "es2022",
5+
"lib": ["es2024", "DOM", "DOM.Iterable"],
56
"moduleResolution": "bundler",
67
"declaration": true,
78
"noEmit": true,

0 commit comments

Comments
 (0)