Leif
Leif

Reputation: 345

Simple command line prompt in Javascript (Node.js)

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.

Upvotes: 3

Views: 988

Answers (1)

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

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
  }
);

Upvotes: 1

Related Questions