Skip to content

Commit f2f0458

Browse files
authored
fix: rewrite then to be an async method (#3)
Rewrites the `then` method to be an async method rather than nesting a `new Promise`.
1 parent 634f900 commit f2f0458

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

eslint.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@ export default [
1010
]
1111
},
1212
eslintConfigs.recommended,
13-
...tseslintConfigs.strict
13+
...tseslintConfigs.strict,
14+
{
15+
rules: {
16+
'@typescript-eslint/no-unused-vars': 'off'
17+
}
18+
}
1419
];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"scripts": {
1414
"build": "tsc --noEmit",
15-
"test": "npm run build && node --test dist/esm/test",
15+
"test": "npm run prepare && echo \"no tests yet\"",
1616
"lint": "eslint src",
1717
"format": "prettier --write src",
1818
"format:check": "prettier --check src",

src/main.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -154,39 +154,36 @@ export class ExecProcess implements Result {
154154
await this._processClosed;
155155
}
156156

157-
public then<TResult1 = Output, TResult2 = never>(
157+
public async then<TResult1 = Output, TResult2 = never>(
158158
onfulfilled?: ((value: Output) => TResult1 | PromiseLike<TResult1>) | null,
159159
_onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
160-
): PromiseLike<TResult1 | TResult2> {
161-
return new Promise<TResult1 | TResult2>(async (resolve, reject) => {
162-
if (this._options?.stdin) {
163-
await this._options.stdin;
164-
}
160+
): Promise<TResult1 | TResult2> {
161+
if (this._options?.stdin) {
162+
await this._options.stdin;
163+
}
165164

166-
const proc = this._process;
165+
const proc = this._process;
167166

168-
if (!proc) {
169-
reject(new Error('No process was started'));
170-
return;
171-
}
167+
if (!proc) {
168+
throw new Error('No process was started');
169+
}
172170

173-
const [stderr, stdout] = await Promise.all([
174-
proc.stderr && readStreamAsString(proc.stderr),
175-
proc.stdout && readStreamAsString(proc.stdout),
176-
this._processClosed
177-
]);
178-
179-
const result: Output = {
180-
stderr: stderr ?? '',
181-
stdout: stdout ?? ''
182-
};
183-
184-
if (onfulfilled) {
185-
resolve(onfulfilled(result));
186-
} else {
187-
resolve(result as TResult1);
188-
}
189-
});
171+
const [stderr, stdout] = await Promise.all([
172+
proc.stderr && readStreamAsString(proc.stderr),
173+
proc.stdout && readStreamAsString(proc.stdout),
174+
this._processClosed
175+
]);
176+
177+
const result: Output = {
178+
stderr: stderr ?? '',
179+
stdout: stdout ?? ''
180+
};
181+
182+
if (onfulfilled) {
183+
return onfulfilled(result);
184+
} else {
185+
return result as TResult1;
186+
}
190187
}
191188

192189
public spawn(): void {
@@ -214,14 +211,7 @@ export class ExecProcess implements Result {
214211
const {command: normalisedCommand, args: normalisedArgs} =
215212
normaliseCommandAndArgs(this._command, this._args);
216213

217-
let handle;
218-
219-
try {
220-
handle = spawn(normalisedCommand, normalisedArgs, nodeOptions);
221-
} catch (err) {
222-
// TODO (jg): handle errors
223-
throw err;
224-
}
214+
const handle = spawn(normalisedCommand, normalisedArgs, nodeOptions);
225215

226216
this._process = handle;
227217
handle.once('error', this._onError);

0 commit comments

Comments
 (0)