3

Is there an easy way to do a simple command line prompt in Node.Js, similar to raw_input in Python?

I've been struggling with trying to get prompt() and readline() to work. My code is simple; I'm trying to iterate over an array and when a match is found, prompt the user for command line input and replace that location in the array with the user input.

1
  • 1
    you can use this project: npmjs.com/package/prompt I think it will provide you with what you are looking for. Commented May 7, 2015 at 4:41

1 Answer 1

1

Use something like async.mapSeries:

var async = require('async'),
    prompt = require('prompt');

var arr = ["A", "match", "b"];

async.mapSeries(arr,
  function iterator(item, next) {
    if (item === "match") {
      return next(null, item);
    }

    console.log('Replacing ' + item);
    prompt.start();
    prompt.get(['val'], function (err, result) {
      prompt.pause();
      return next(err, result.val);
    });
  },
  function callback(err, updatedArr) {
    if (!err) // do something with updatedArr
  }
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.