Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add web APIs for alert, confirm and prompt to Node.js #174

Open
iansu opened this issue Feb 20, 2023 · 2 comments
Open

Add web APIs for alert, confirm and prompt to Node.js #174

iansu opened this issue Feb 20, 2023 · 2 comments

Comments

@iansu
Copy link
Contributor

iansu commented Feb 20, 2023

Deno supports the alert, confirm and prompt web APIs to read input from the user in a terminal context. I think this would be great to have in Node.js as well. Deno documentation is here: https://examples.deno.land/prompts

You can achieve the same thing in Node with readline but it's not as nice of an API. For example, here's how you prompt the user for input using readline from the Node docs (https://nodejs.org/api/readline.html#readline):

const readline = require('node:readline');
const { stdin: input, stdout: output } = require('node:process');

const rl = readline.createInterface({ input, output });

rl.question('What do you think of Node.js? ', (answer) => {
  // TODO: Log the answer in a database
  console.log(`Thank you for your valuable feedback: ${answer}`);

  rl.close();
});

And here's how you would do the same thing with prompt:

const answer = prompt('What do you think of Node.js?');

console.log(`Thank you for your valuable feedback: ${answer}`);

I think these new APIs would be very useful for tool authors and would also bring more web APIs to Node.js.

One possible difficulty implementing these APIs is that they seem to be synchronous in Deno and readline does not work synchronously. I've created a basic POC of these functions using readline (and await) as a starting point here: https://github.com/iansu/node-prompts

@ljharb
Copy link
Member

ljharb commented Feb 20, 2023

The functionality seems awesome; I would hope node wouldn't add them as globals with those names, though, since it will likely break (or at best, complicate) environment detection code.

@iansu
Copy link
Contributor Author

iansu commented Mar 8, 2023

If we were to add these in a module do you think it would still be worthwhile to make the API compatible with the browser implementations? This could help decide if these functions should be synchronous or asynchronous as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants