Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
Created August 14, 2025 10:02
Show Gist options
  • Select an option

  • Save lejonmanen/ee3fcd2c1671048c23053411497867a6 to your computer and use it in GitHub Desktop.

Select an option

Save lejonmanen/ee3fcd2c1671048c23053411497867a6 to your computer and use it in GitHub Desktop.
Node async readline
/* Exempel på användning:
import { ask } from './ask.ts'
let x: string = await ask('Vilken är din favoritfärg? ')
console.log('Du svarade: ' + x)
*/
import readline from 'node:readline'
async function ask(query: string): Promise<string> {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, tabSize: 4 });
return new Promise((resolve) => rl.question(query, (answer) => {
rl.close();
resolve(answer);
}));
}
export { ask }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment