Last active
July 15, 2022 01:10
-
-
Save jebeck/1ca224af150767a25ecefc020d29bbd0 to your computer and use it in GitHub Desktop.
infinite interactivity with Inquirer and RxJS's Subject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run with `node infinite.js` in node v4.x+ | |
// must have Inquirer installed (`npm install inquirer`) | |
const inquirer = require('inquirer'); | |
const Rx = require('rx'); | |
const prompts = new Rx.Subject(); | |
function makePrompt(msg) { | |
return { | |
type: 'input', | |
name: `userInput-${i}`, | |
message: `${msg || 'Say something to start chatting!'}\n\n`, | |
}; | |
} | |
let i = 0; | |
inquirer.prompt(prompts).ui.process.subscribe(({ answer }) => { | |
if (answer !== '') { | |
i += 1; | |
prompts.onNext(makePrompt(`This is prompt #${i}.`)); | |
} else { | |
prompts.onCompleted(); | |
} | |
}, (err) => { | |
console.warn(err); | |
}, () => { | |
console.log('Interactive session is complete. Good bye! 👋\n'); | |
}); | |
prompts.onNext(makePrompt()); |
@yunhan0 Put a try catch around inquirer.prompt and I get this error TypeError: dest.addListener is not a function
. Looks unavoidable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@yunhan0 thanks, I will try. Looks like new API