prompt-async

The enhanced prompt module that enables you to `await` its methods.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
prompt-async
0.9.95 years ago5 years agoMinified + gzip package size for prompt-async in KB

Readme

The enhanced prompt module that enables you to await its methods.

Example Try it now!

With prompt-async
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();
The obsolete way (causes a callback hell)
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();

Frequently answered questions

I've been working with prompt. Do I have to modify the existing code to use this?

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.

Any bugs, suggestions, or questions?

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 or send an e-mail to me: wlzla000@naver.com.
Feel free to contribute!
Thank you.

Licensed under

the MIT license.