вот пример бесконечного цикла не блокирующего Event loop
Выход из цикла while по нажатию на кнопку Esc
Выход из программы по нажатию по Ctrl + c
const readline = require('readline');
const sleep = require('await-sleep');
let run = true;
let timer = 0;
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
console.log(str, key, run);
// Conditions on key
if(key.name == 'escape'){
run = false;
}
if(key.name == 'c' && key.ctrl == true){
process.exit(1);
}
})
async function init(){
while (run){
await sleep(1000);
timer++;
console.log('timer',timer);
}
}
init()