I’m not able to properly handle an user abortion of a promise readline question with SIGINT or Ctrl+D while rl.question waits for an user input:
import * as readline from 'node:readline/promises';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await rl.question('>>> ');
The abortion results in:
Warning: Detected unsettled top-level await at file:///foo/bar/test.js:xx
const answer = await rl.question('>>> ');
I can mimic the intended behaviour with non promise readline like:
import * as readline from 'node:readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let rej = () => {};
rl.on('close', () => {
console.log('closing');
rej();
});
const answer = await new Promise((r, j) => {
rej = j;
rl.question('>>> ', r);
}).catch(() => console.error('interrupted'));
How to safely handle such top level unsettled promises to be able to use the node:readline/promises interface?
Environment:
Node v22.2.0, ES module