You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
constreadline=require('node:readline');const{stdin: input,stdout: output}=require('node:process');constrl=readline.createInterface({ input, output });rl.question('What do you think of Node.js? ',(answer)=>{// TODO: Log the answer in a databaseconsole.log(`Thank you for your valuable feedback: ${answer}`);rl.close();});
And here's how you would do the same thing with prompt:
constanswer=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
The text was updated successfully, but these errors were encountered:
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.
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.
Deno supports the
alert
,confirm
andprompt
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/promptsYou 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 usingreadline
from the Node docs (https://nodejs.org/api/readline.html#readline):And here's how you would do the same thing with
prompt
: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 usingreadline
(andawait
) as a starting point here: https://github.com/iansu/node-promptsThe text was updated successfully, but these errors were encountered: