tj / co-prompt

sane terminal user-input for node.js using thunks / generators
192 stars 23 forks source link

prompt in an array #12

Open stlouisweb opened 8 years ago

stlouisweb commented 8 years ago

I want to loop through an array and create a series of prompts based off of this array. I seem to be having trouble stopping the loop in order to collect each prompt sequentially. For Example, if I have an array named DefaultArray, I want to create a prompt for each item in the DefaultArray:

var co = require('co');
var forEach = require('co-foreach');
var prompt = require('co-prompt');

forEach(defaultVars, function * (item, idx) {
    // do something awesome with generators
    var value = yield prompt(item + ': ');
    console.log(value);
}).then(handleFinish);

Suppose DefaultArray = ['Do you like Apples?', 'Do you like Oranges?'];

I would expect to see: Do you like Apples?: yes yes Do you like Oranges?: no no

Instead I see: Do you like Apples?:Do you like Oranges?: yes yes yes

bxt commented 7 years ago

I had this problem, too. Looking around for solution, I finally resented to just using a good old for loop like this:

co(function* () { // assuming you are somewhere in a co'ed function
  for (let i = 0; i < defaultVars, i++)
      // do something awesome with generators
      var value = yield prompt(defaultVars[i] + ': ');
      console.log(value);
  }
  yield handleFinish;
});

It would be nice if some Javascript-ninja had a better solution though.