Waiting for user to enter input in Node.js -


i understand rationale in node.js asynchronous events , learning how write code way. however, stuck following situation:

i want write code pauses user input.

the program not intended server (though it's intended command line). realize atypical use of node. goal migrate program client-side javascript application, find working in node.js both fascinating , useful debugging. brings me example illustrates problem:

it reads in text file , outputs each line unless line ends "?". in case, should pause user clarify meant line. program outputs lines first , waits clarifications @ end.

is there way force node.js pause command-line input precisely cases conditional fires (i.e., line ends "?")?

var fs = require("fs"); var filename = ""; var = 0; var lines = [];  // modeled on http://st-on-it.blogspot.com/2011/05/how-to-read-user-input-with-nodejs.html var query = function(text, callback) {     process.stdin.resume();     process.stdout.write("please clarify meant by: " + text);     process.stdin.once("data", function(data) {         callback(data.tostring().trim());     }); };  if (process.argv.length > 2) {     filename = process.argv[2];     fs.readfile(filename, "ascii", function(err, data) {         if (err) {             console.error("" + err);             process.exit(1);         }         lines = data.split("\n");         (i = 0; < lines.length; i++) {             if (/\?$/.test(lines[i])) { // ask user clarification                 query(lines[i], function(response) {                     console.log(response);                     process.stdin.pause();                 });             }             else {                 console.log(lines[i]);             }         }     }); } else {     console.error("file name must supplied on command line.");     process.exit(1); }   

the trick not itteratively, loop recursively. next line printout in callback, called either a: after line gets printed, or b: after console input has been processed.

var fs = require("fs");  // modeled on http://st-on-it.blogspot.com/2011/05/how-to-read-user-input-with-nodejs.html function query(text, callback) {     'use strict';     process.stdin.resume();     process.stdout.write("please clarify meant by: " + text);     process.stdin.once("data", function (data) {         callback(data.tostring().trim());     }); }  function printlineswaitforquestions(lines, somecallbackfunction) {     'use strict';      function continueprocessing() {         if (lines.length) {             printnextline(lines.pop());         } else {             somecallbackfunction();         }     }      function printnextline(line) {          if (/\?$/.test(line)) { // ask user clarification             query(line, function (response) {                 console.log(response);                 process.stdin.pause();                 continueprocessing();             });         } else {             console.log(line);             continueprocessing();         }     }      continueprocessing(); }  if (process.argv.length > 2) {     var filename = process.argv[2];     fs.readfile(filename, "ascii", function (err, data) {         'use strict';          if (err) {             console.error("" + err);             process.exit(1);         }          var lines = data.split("\n");         printlineswaitforquestions(lines, function () {             console.log('were done now');         });     }); } else {     console.error("file name must supplied on command line.");     process.exit(1); } 

this solution 2 reasons:

  1. it's relatively clean , entire process can contained within own function closure, potentially leading modularization.

  2. it doesn't break other asynchronous things may want do.

    there no iterative waiting loop , 1 async task being launched per array of lines have. if, in version, had millions of lines? have spun millions of async outputs instantaneously... bad!

    recursive method not allows better concurrency of other async work want do, don't clog event loop mini async tasks 1 function call. can cause memory issues, performance degradation, , other issues worthy of avoiding, particularly on large inputs.

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -