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

Spinner pauses when using child_process.execSync #86

Closed
TotomInc opened this issue Aug 12, 2018 · 8 comments
Closed

Spinner pauses when using child_process.execSync #86

TotomInc opened this issue Aug 12, 2018 · 8 comments

Comments

@TotomInc
Copy link

Spinner pauses when I start a command using child_process.execSync.

Is this intended? Did I need to manually render the frames using ora.render()?

@sindresorhus
Copy link
Owner

JavaScript is single-threaded, so running a synchronous function blocks everything else, including Ora. Use exec instead.

@eli8levit
Copy link

eli8levit commented Mar 11, 2019

If so, why it does work and doesn't pause the spinner?

async function run() {
  spinner.start('Installing packages...');
  await install('npm install');
  spinner.succeed('Installed');
}

function install() {
  return new Promise((resolve, reject) => {
    exec('npm install').on('close', code => {
      if (code === 0) {
        resolve();
      }
      reject();
    });
  });
}

run();

@sindresorhus
Copy link
Owner

@jediyozh Because exec is asynchronous, not synchronous.

@eli8levit
Copy link

@sindresorhus of course but I made it synchronous with Promise, no?

@sindresorhus
Copy link
Owner

@jediyozh No. I would recommend reading up on how promises and async/await work. In short, await only pauses the execution context of the function, not the whole process.

@eli8levit
Copy link

@sindresorhus Got it. Thanks for the explanation and for the great tool 👍

@sindresorhus
Copy link
Owner

Also, I would recommend execa instead of exec. It's Promise-friendly and more user-friendly.

@eli8levit
Copy link

@sindresorhus I will look at this

Repository owner deleted a comment from famanoder Nov 26, 2019
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

3 participants