I need a function that will pause the execution of the script until a key is pressed. I've tried:
var stdin = process.openStdin();
require('tty').setRawMode(true);
stdin.on('keypress', function (chunk, key) {
process.stdout.write('Get Chunk: ' + chunk + '\n');
if (key && key.ctrl && key.name == 'c') process.exit();
});
but it's just listening for a keypress and nothing happens. The program does not continue executing.
How can I pause execution?
You use repeating to print repeating strings: const repeating = require('repeating'); console. log(repeating(100, 'unicorn '));
The next() function is not a part of the Node. js or Express API, but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention. To load the middleware function, call app.
Use window. stop() to prevent the page from loading and running. For NodeJS only – Use process. abort() or process.
keys() The NodeList. keys() method returns an iterator allowing to go through all keys contained in this object. The keys are unsigned integer .
In node.js 7.6 and later you can do this:
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once('data', () => {
process.stdin.setRawMode(false)
resolve()
}))
}
;(async () => {
console.log('program started, press any key to continue')
await keypress()
console.log('program still running, press any key to continue')
await keypress()
console.log('bye')
})().then(process.exit)
Or if you want CTRL-C to exit the program but any other key to continue normal execution, then you can replace the "keypress" function above with this function instead:
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once('data', data => {
const byteArray = [...data]
if (byteArray.length > 0 && byteArray[0] === 3) {
console.log('^C')
process.exit(1)
}
process.stdin.setRawMode(false)
resolve()
}))
}
Works for me:
console.log('Press any key to exit');
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
The accepted solution waits asynchronously for a key event and then exits, it is not really a solution to "Press any key to continue".
I needed to pause while writing some nodejs shell scripts. I ended up using the spawnSync of the child_process with the shell command "read".
This will basically pause the script and when you press Enter it will continue. Much like the pause command in windows.
require('child_process').spawnSync("read _ ", {shell: true, stdio: [0, 1, 2]});
Hope this helps.
This snippet does the job if you don't want to exit the process:
console.log('Press any key to continue.');
process.stdin.once('data', function () {
continueDoingStuff();
});
It's async so won't work inside loop as-is-- if you're using Node 7 you could wrap it in a promise and use async/await
.
There is a package for this: press-any-key
And here is an example:
const pressAnyKey = require('press-any-key');
pressAnyKey("Press any key to resolve, or CTRL+C to reject", {
ctrlC: "reject"
})
.then(() => {
console.log('You pressed any key')
})
.catch(() => {
console.log('You pressed CTRL+C')
})
It runs without problems in W10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With