Skip to content

Commit

Permalink
fix: rewrite then to be an async method (#3)
Browse files Browse the repository at this point in the history
Rewrites the `then` method to be an async method rather than nesting a
`new Promise`.
  • Loading branch information
43081j authored Jun 14, 2024
1 parent 634f900 commit f2f0458
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
7 changes: 6 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ export default [
]
},
eslintConfigs.recommended,
...tseslintConfigs.strict
...tseslintConfigs.strict,
{
rules: {
'@typescript-eslint/no-unused-vars': 'off'
}
}
];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"scripts": {
"build": "tsc --noEmit",
"test": "npm run build && node --test dist/esm/test",
"test": "npm run prepare && echo \"no tests yet\"",
"lint": "eslint src",
"format": "prettier --write src",
"format:check": "prettier --check src",
Expand Down
62 changes: 26 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,39 +154,36 @@ export class ExecProcess implements Result {
await this._processClosed;
}

public then<TResult1 = Output, TResult2 = never>(
public async then<TResult1 = Output, TResult2 = never>(
onfulfilled?: ((value: Output) => TResult1 | PromiseLike<TResult1>) | null,
_onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null
): PromiseLike<TResult1 | TResult2> {
return new Promise<TResult1 | TResult2>(async (resolve, reject) => {
if (this._options?.stdin) {
await this._options.stdin;
}
): Promise<TResult1 | TResult2> {
if (this._options?.stdin) {
await this._options.stdin;
}

const proc = this._process;
const proc = this._process;

if (!proc) {
reject(new Error('No process was started'));
return;
}
if (!proc) {
throw new Error('No process was started');
}

const [stderr, stdout] = await Promise.all([
proc.stderr && readStreamAsString(proc.stderr),
proc.stdout && readStreamAsString(proc.stdout),
this._processClosed
]);

const result: Output = {
stderr: stderr ?? '',
stdout: stdout ?? ''
};

if (onfulfilled) {
resolve(onfulfilled(result));
} else {
resolve(result as TResult1);
}
});
const [stderr, stdout] = await Promise.all([
proc.stderr && readStreamAsString(proc.stderr),
proc.stdout && readStreamAsString(proc.stdout),
this._processClosed
]);

const result: Output = {
stderr: stderr ?? '',
stdout: stdout ?? ''
};

if (onfulfilled) {
return onfulfilled(result);
} else {
return result as TResult1;
}
}

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

let handle;

try {
handle = spawn(normalisedCommand, normalisedArgs, nodeOptions);
} catch (err) {
// TODO (jg): handle errors
throw err;
}
const handle = spawn(normalisedCommand, normalisedArgs, nodeOptions);

this._process = handle;
handle.once('error', this._onError);
Expand Down

0 comments on commit f2f0458

Please sign in to comment.