Inquirer.js: The way to interrupt prompt

Created on 24 Aug 2016  路  5Comments  路  Source: SBoudrias/Inquirer.js

We need the way to interrupt prompt, Node.js is asynchronous library, anything can happen in async world, so not having a function to interrupt prompt is bad.

I created my own implementation here:

https://github.com/ro31337/Inquirer.js#modified-by-roman-pushkin

Commit:

https://github.com/ro31337/Inquirer.js/commit/954f94616ba9d0e997be738446001354791010ec

Wanted to discuss this approach, but it works and I use it already in my project(s).

Basically, I just created event emitter and when prompt is interrupted I use red color to display "interrupted" word. Callback is not executed.

It can be improved by replacing inquirer.pubsub.emit('interrupt-prompt'); with inquirer.interruptPrompt(); or similar.

Please tell me what you think, and if you feel enthusiastic, go ahead and create pull request for all prompts based on my PoC changes.

Most helpful comment

I feel rejecting the promise would be a better default behavior.

All 5 comments

UPD: I have some more work in my branch. Now I just return undefined as answers when things were interrupted.

Hey, so the latest version reject the promise when you interrupt the prompt session right?

I'd like to merge that in. Mind sending a pull request?

@SBoudrias well, it does not reject the promise (it does internally, however). So inside of

.then((answers) => { ... })

I just check answers to be truthy.

Moreover, interrupt handler implemented for list only. There is some work required for other types of prompts. I don't have too much time now, can you tell me your preference, what's better:

  • Reject the promise
  • Resolve the promise with undefined answers

?

I feel rejecting the promise would be a better default behavior.

@ro31337 @SBoudrias What if the user has answered some of the questions but not all? In the case of an interrupt, I can imagine wanting to get the answers that were already entered. I could detect the interrupt case by checking whether there are fewer answers than questions.

So I would suggest resolving the promise, always when an array of (zero or more) answers, which could be checked against the question count to determine whether the user completed all the questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aydn picture aydn  路  5Comments

XhmikosR picture XhmikosR  路  7Comments

bapti picture bapti  路  6Comments

jahvi picture jahvi  路  6Comments

adalinesimonian picture adalinesimonian  路  6Comments