0

Node.js で fdupes のようなシナリオを実装することを想像してください。無理そうです。これに対する人々の提案は何ですか?

npm で「prompt」モジュールを使用します。これはおおよそ私のコードがどのように見えるかです:

for(var i = 0; i < 50; i++) {
  log('shit up console with lots of messy and space-consuming output, pushing everything else off the screen.');
  prompt.get('foo', function(err, resp){
    doSomethingWith(resp.foo);
  });
}

ユーザーが最初の応答を入力する時間さえ持てないうちに、さらに 50 セットの出力が、画面外での 2 番目の応答について決定を下すために必要な情報を詰め込みました。

これは、ノードのあまりにヒップなシンクロニシティ ギミックの大きな失敗のように思えますね。何か不足していますか?

4

2 に答える 2

2

If you want to loop over some asynchronous functions, you should try using async's timesSeries, which applies a function to n times in series. If any function returns an error, a main error handler will be called.

Here is an example, using your code:

var async = require('async');
async.timesSeries(50, function (n, next) {
  prompt.get('foo', function  (err, res) {
    var value = doSomethingWith(resp.foo);
    if (value !== 'bar') next(new Error('value !== bar'));
    else next(null, value); 
  }
}, function (err, res) {
  // err !== null
  // or res is an array with 50 elements
});
于 2013-11-08T13:20:16.560 に答える