node-seq編集
node.js以前の記事 node.jsを使ったチャットアプリケーションとnode-seqの紹介 - wktkWebDiary でnode-seqの紹介をしましたが、
そこでGitHub - substack/node-seq: Chainable asynchronous flow control for node.js with sequential and parallel primitives and pipeline-style error handlingの出番です。
このモジュールを使うとコールバックのネストが次のようにすっきりします。
ただこのままだとエラーをcatchしたあとに続く処理も行われてしまうため、一工夫する必要みたいです。※改良中
というわけで、forkして編集しました。
上記以外のバグも修正した安定版のstableブランチにまとめてあります。
https://github.com/YaaaaaSuuuuu/node-seq
GitHub - yasuoza/node-seq: Chainable asynchronous flow control for node.js with sequential and parallel primitives and pipeline-style error handling (user.name変えました)
この編集で、エラーをキャッチした所で次のチェーンには進まなくなりました。
これで次のように安心してエラーハンドリングできそうです。
var Seq = require('seq'); function lib (args, cb) { var error = null; if (args == undefined) { error = new Error('args is neccesary'); } cb(error, args); } Seq() .seq(function () { lib(undefined, this); }) .catch(function (err) { console.log(err); }) .seq(function () { throw new Error('You must edit node-seq'); // This step is ignored. }) ;
テストもexpressoだったものを最近増えてきているmochaで書き直しました。
インストール方法は
node_modulesディレクトリで
$ git clone git://github.com/YaaaaaSuuuuu/node-seq.git $ npm install -d
で行えます。
テストはstableブランチにcheckoutした後に
$ mocha
をして下さい。