Skip to content

Commit 48c7fc9

Browse files
refactor: use native Promise.withResolvers
1 parent bceb13a commit 48c7fc9

5 files changed

Lines changed: 8 additions & 18 deletions

File tree

src/prompts/confirm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { styleText } from "node:util";
55

66
// Import Internal Dependencies
77
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.ts";
8-
import { stringLength, withResolvers } from "../utils.ts";
8+
import { stringLength } from "../utils.ts";
99
import { SYMBOLS } from "../constants.ts";
1010

1111
export interface ConfirmOptions extends AbstractPromptOptions {
@@ -128,7 +128,7 @@ export class ConfirmPrompt extends AbstractPrompt<boolean> {
128128
this.write(SYMBOLS.HideCursor);
129129

130130
try {
131-
const { resolve, promise } = withResolvers<boolean>();
131+
const { resolve, promise } = Promise.withResolvers<boolean>();
132132
const questionQuery = this.#getQuestionQuery();
133133

134134
this.write(questionQuery);

src/prompts/multiselect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { styleText } from "node:util";
44

55
// Import Internal Dependencies
66
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.ts";
7-
import { stringLength, withResolvers } from "../utils.ts";
7+
import { stringLength } from "../utils.ts";
88
import { SYMBOLS } from "../constants.ts";
99
import { isValid, type PromptValidator, resultError } from "../validators.ts";
1010
import { type Choice } from "../types.ts";
@@ -370,7 +370,7 @@ export class MultiselectPrompt<T extends string> extends AbstractPrompt<T> {
370370

371371
render({ initialRender: true });
372372

373-
const { promise, resolve } = withResolvers<T[]>();
373+
const { promise, resolve } = Promise.withResolvers<T[]>();
374374
this.#boundKeyPressEvent = this.#onKeypress.bind(this, resolve, render);
375375
this.stdin.on("keypress", this.#boundKeyPressEvent);
376376

src/prompts/question.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { styleText } from "node:util";
44

55
// Import Internal Dependencies
66
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.ts";
7-
import { stringLength, withResolvers } from "../utils.ts";
7+
import { stringLength } from "../utils.ts";
88
import { SYMBOLS } from "../constants.ts";
99
import { isValid, type PromptValidator, resultError } from "../validators.ts";
1010

@@ -55,7 +55,7 @@ export class QuestionPrompt extends AbstractPrompt<string> {
5555
}
5656

5757
#question(): Promise<string> {
58-
const { resolve, promise } = withResolvers<string>();
58+
const { resolve, promise } = Promise.withResolvers<string>();
5959

6060
const questionQuery = this.#getQuestionQuery();
6161

src/prompts/select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { styleText } from "node:util";
44

55
// Import Internal Dependencies
66
import { AbstractPrompt, type AbstractPromptOptions } from "./abstract.ts";
7-
import { stringLength, withResolvers } from "../utils.ts";
7+
import { stringLength } from "../utils.ts";
88
import { SYMBOLS } from "../constants.ts";
99
import { isValid, type PromptValidator, resultError } from "../validators.ts";
1010
import { type Choice } from "../types.ts";
@@ -310,7 +310,7 @@ export class SelectPrompt<T extends string> extends AbstractPrompt<T> {
310310

311311
render({ initialRender: true });
312312

313-
const { resolve, promise } = withResolvers<T>();
313+
const { resolve, promise } = Promise.withResolvers<T>();
314314
this.#boundKeyPressEvent = this.#onKeypress.bind(this, resolve, render);
315315
this.stdin.on("keypress", this.#boundKeyPressEvent);
316316

src/utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,3 @@ export function stringLength(
4444
return length;
4545
}
4646

47-
export function withResolvers<T>() {
48-
let resolve!: (value: T | PromiseLike<T>) => void;
49-
let reject!: (reason?: unknown) => void;
50-
const promise = new Promise<T>((res, rej) => {
51-
resolve = res;
52-
reject = rej;
53-
});
54-
55-
return { promise, resolve, reject };
56-
}

0 commit comments

Comments
 (0)