prompt-async
Version:
The enhanced prompt module that enables you to `await` its methods.
117 lines (87 loc) • 3.17 kB
Markdown
The **enhanced** [`prompt`](https://www.npmjs.com/package/prompt) module that enables you to [**`await`** its methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function).
```javascript
const prompt = require("prompt-async");
async function example_async() // Available only with `prompt-async`!
{
// Start the prompt.
prompt.start();
// Get two properties from the user: the `username` and `email`.
const {username, email} = await prompt.get(["username", "email"]);
// Get two properties from the user: the `password` and `food`.
const {password, food} = await prompt.get(["password", "food"]);
// Log the results.
console.log("Command-line input received: ");
console.log(` username: ${username},`);
console.log(` password: ${password},`);
console.log(` email: ${email},`);
console.log(` food: ${food}.`);
}
async function error_handling_async()
{
try
{
await example_async();
}
catch(error)
{
console.error("An error occurred: ", error);
}
}
error_handling_async();
```
```javascript
const prompt = require("prompt-async");
function example() // Still supports the obsolete way.
{
// Start the prompt.
prompt.start();
// Get two properties from the user: the `username` and `email`.
prompt.get(["username", "email"], (error, result1) => {
if(error)
{
throw Error(error);
}
// Get two properties from the user: the `password` and `food`.
prompt.get(["password", "food"], (error, result2) => {
if(error)
{
throw Error(error);
}
// Log the results.
console.log("Command-line input received: ");
console.log(` username: ${result1.username},`);
console.log(` password: ${result2.password},`);
console.log(` email: ${result1.email},`);
console.log(` food: ${result2.food}.`);
});
});
}
function error_handling()
{
try
{
example();
}
catch(error)
{
console.error("An error occurred: ", error);
}
}
error_handling();
```
**The only thing you have to do is to replace `require("prompt")` to `require("prompt-async")`.**
As you can see above, it has backward-compatibility, so it would work well with your `prompt` code.
However, please be aware that your `prompt` code pattern is discouraged, and try to use _`async`-`await`_ pattern.
If you found a bug on it, or if you want to suggest something to improve it, or if you have a question,
please [leave an issue on the _GitHub_ repository](https://github.com/wlzla000/prompt-async/issues) or send an e-mail to me: `wlzla000@naver.com`.
**Feel free to contribute!**
Thank you.
[](https://github.com/wlzla000/prompt-async/blob/master/LICENSE).