1
1

More than 5 years have passed since last update.

javascriptのcallbackのネストの可読性を高めるイディオム

Last updated at Posted at 2012-07-13

[f1,f2,f3]みたいな感じでcallbackのネストの順番を配列で表現するためのイディオム

Array型拡張用メソッドの追加
Array.prototype.method = (name,func)->
  @prototype[name] = func
  @
cb_nestの実装
Array.method 'cb_nest',()->  
  if @length
    f = @pop()
    f(@cb_nest.call @)
``````coffeescript:使い方
f1 = (data)->
  console.log 1
  data
f2 = (data)->
  console.log 2
  data
f3 = (data)->
  console.log 3
  data
finish = ()->
  console.log "finish"
f_arr = [f1,f2,f3,finish]

f_arr.cb_nest(finish)
#1
#2
#3
#"finish"
```たぶんうごきます。
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1