Callbackの処理が簡単な場合は、この階段状のコードでも特に問題ないだが、もし複雑の処理になると、かなり読みづらい。そこで、EventEmitterを使って処理結果をEventとして外に出すのが解決方の一つ。 EventEmitterの使い方 EventEmitterはnode.jseventsモジュールの中に入ってる。EventEmitterを継承することで、onとemitメソードでEventの受け取りと転送ができる。サンプルを作成してみよう:
Nodeの組み込みモジュール・サードパーティモジュール問わず広く使われるのが EventEmitter オブジェクト。 これはNodeで使われるデザインパターンの筆頭みたいなものなので、知っておかねばならない。 ドキュメント: Events ブラウザ上のJavaScriptで addEventListener を使ってイベントドリブンの開発を行うが、Node上でそれを行うのための機能を提供するのが EventEmitter。 例えば次のように使う var EventEmitter = require('events').EventEmitter; function asyncFunc() { var ev = new EventEmitter; console.log('in asyncFunc'); setTimeout(function () { ev.emit('done', 'fo
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く