Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spawnAsync blocks #340

Closed
trim21 opened this issue Jun 26, 2021 · 0 comments
Closed

spawnAsync blocks #340

trim21 opened this issue Jun 26, 2021 · 0 comments

Comments

@trim21
Copy link
Collaborator

trim21 commented Jun 26, 2021

const spawnAsync = (cmd: string, args: string[], options: SpawnSyncOptions): Promise<string> =>
new Promise((resolve, reject) => {
const {stdout, error} = spawnSync(cmd, args, options);
if (error) {
reject(error);
return;
}

this didn't do anything Async, spawnSync will block event loop when executing this command.

ref:

https://nodejs.org/en/docs/guides/dont-block-the-event-loop/

In a server, you should not use the following synchronous APIs from these modules:
...
Child process:
child_process.spawnSync
...

here is a promise example of spawn API

import {spawn} from 'child_process';
// *** Not async
export default function clone(options: { url: string; path: string; }) {
  // *** Return the promise
  return new Promise(function (resolve, reject) {
    const url = options.url;
    const target = options.path;
    const args = ['clone', url, target];
    const process = spawn('git', args);
    process.on('close', function (code) { // Should probably be 'exit', not 'close'
      // *** Process completed
      resolve(code);
    });
    process.on('error', function (err) {
      // *** Process creation failed
      reject(err);
    });
  });
}
@trim21 trim21 changed the title spawnAsync is block spawnAsync blocks Jun 26, 2021
jesec added a commit that referenced this issue Jun 26, 2021
jesec added a commit that referenced this issue Jun 26, 2021
@jesec jesec closed this as completed Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants