Skip to content

Instantly share code, notes, and snippets.

@danyelhenrique
Created April 28, 2023 00:14
Show Gist options
  • Select an option

  • Save danyelhenrique/6fc68e9eafccc0047912c366d89fb675 to your computer and use it in GitHub Desktop.

Select an option

Save danyelhenrique/6fc68e9eafccc0047912c366d89fb675 to your computer and use it in GitHub Desktop.
nodejs spawn promisse async
#!/usr/bin/env node
/**
* @param {string} command process to run
* @param {string[]} args command line arguments
* @returns {Promise<void>} promise
*/
const runCommand = (command, args) => {
const cp = require("child_process");
return new Promise((resolve, reject) => {
const executedCommand = cp.spawn(command, args, {
stdio: "inherit",
shell: true
});
executedCommand.on("error", error => {
reject(error);
});
executedCommand.on("exit", code => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment