Skip to content

Instantly share code, notes, and snippets.

@dianjuar
Created November 15, 2022 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dianjuar/339347273d3b25fa10bdd55ccc8db58f to your computer and use it in GitHub Desktop.
Save dianjuar/339347273d3b25fa10bdd55ccc8db58f to your computer and use it in GitHub Desktop.
child_process.spawn using promises
import { spawn } from 'child_process';
export function spawnAsync(command: string, args?: string[]): Promise<void> {
return new Promise((resolve, reject) => {
const process = spawn(command, args);
process.stdout.on('data', data => {
console.log(data.toString());
});
process.stderr.on('data', data => {
console.log(data.toString());
});
process.on('close', code => {
if (code === 0) {
resolve();
} else {
reject(code);
}
});
process.on('error', reject);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment