私は初心者のJavaScripterであり、V8エンジンの内部で何が起こっているのかを実際に知りません。
そうは言っても、node.js環境への初期の取り組みを本当に楽しんでいますが、グローバルイベントを生成する手段として、常にイベントを使用しています。 Objective-CまたはPythonプログラムで言うようなパターン。
私はいつもこのようなことをしていることに気付きます:
var events = require('events');
var eventCenter = new events.EventEmitter();
eventCenter.on('init', function() {
var greeting = 'Hello World!';
console.log('We're in the init function!);
eventCenter.emit('secondFunction', greeting);
});
eventCenter.on('secondFunction', function(greeting) {
console.log('We're in the second function!);
console.log(greeting);
eventCenter.emit('nextFunction');
});
eventCenter.on('nextFunction', function {
/* do stuff */
});
eventCenter.emit('init');
したがって、実際には、「非同期」のnode.jsコードを、期待どおりの順序で処理するコードに構造化するだけです。代わりに、それが理にかなっている場合は、「逆コーディング」のようなものです。これをパフォーマンス重視または哲学重視のコールバック重視で行うことに違いはありますか?イベントの代わりにコールバックを使用して同じことを行う方が良いですか?