Nodejsで非同期の処理をチェーンでつなぐユーティリティ
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
自分用に書いてみた
果たしてこれはfs モジュールとかでも動くのか。動いてください。
(jQueryのまね。)
# coffee # sample method that takes callback from the last argument yeah = (str, callback) -> console.log '山!' setTimeout -> console.log '...川!', str callback && callback() , 1000 class Defered constructor: (args...) -> @fnBuf_ = if args[0] instanceof Array then args[0] else args then: (fn) -> @fnBuf_.push fn @ done: (@callback = ->) -> @init_() @fnBuf_[0]() init_: -> for fn in @fnBuf_.reverse() next = if @fnBuf_[_i-1] then @fnBuf_[_i-1] else @callback @fnBuf_[_i] = @fnBuf_[_i].bind(@, next) @fnBuf_.reverse() # utility method to create Defered instance $$ = (args...) -> return new Defered(args) # usage $$(yeah).then(yeah).then(yeah).done( -> console.log 'all ended!!')
追 うごいたよお〜〜
fs = require('fs') $$().then(fs.stat.bind(@, __dirname + '/' + 'data')).done((err, stat) -> console.log stat )